【二叉树层序遍历】【队列】Leetcode 102 107 199 637 429 515
- 102. 二叉树的层序遍历解法 用队列实现
- 107. 二叉树的层序遍历 II解法
- 199. 二叉树的右视图 解法
- 637. 二叉树的层平均值 解法
- 429. N叉树的层序遍历
- 515. 在每个树行中找最大值
---------------🎈🎈102. 二叉树的层序遍历 题目链接🎈🎈-------------------
---------------🎈🎈107. 二叉树的层序遍历 II 题目链接🎈🎈-------------------
---------------🎈🎈199. 二叉树的右视图 题目链接🎈🎈-------------------
---------------🎈🎈637. 二叉树的层平均值 题目链接🎈🎈-------------------
---------------🎈🎈429. N叉树的层序遍历 题目链接🎈🎈-------------------
---------------🎈🎈515. 在每个树行中找最大值 题目链接🎈🎈-------------------
102. 二叉树的层序遍历解法 用队列实现
时间复杂度O(N)
空间复杂度O(N)
import com.sun.source.tree.Tree;/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> result = new ArrayList<>();if(root == null) return result;Queue<TreeNode> myqueue = new LinkedList<>();myqueue.add(root);while(!myqueue.isEmpty()){List<Integer> tempres = new ArrayList<>();int size = myqueue.size(); // 获取当前层的节点数for(int i = 0; i< size; i++){TreeNode temp = myqueue.poll();tempres.add(temp.val);if(temp.left != null){myqueue.add(temp.left);}if(temp.right != null){myqueue.add(temp.right);}}result.add(tempres);}return result;}
}
107. 二叉树的层序遍历 II解法
在基础的层序遍历的基础上增加一个Collections.reverse()
翻转输出即可
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> result = new ArrayList<>();Queue<TreeNode> myqueue = new LinkedList<>();if(root==null) return result;myqueue.add(root);while(!myqueue.isEmpty()){List<Integer> resulttemp = new ArrayList<>();int size = myqueue.size(); for(int i = 0; i< size; i++){TreeNode temp = myqueue.poll();resulttemp.add(temp.val);if(temp.left != null) {myqueue.add(temp.left);}if(temp.right != null) {myqueue.add(temp.right);}}result.add(resulttemp);}Collections.reverse(result);return result;}
}
199. 二叉树的右视图 解法
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public List<Integer> rightSideView(TreeNode root) {List<Integer> result = new ArrayList<>();Queue<TreeNode> myqueue = new LinkedList<>();if(root == null) return result;myqueue.add(root);while(!myqueue.isEmpty()){int size = myqueue.size();for(int i = 0; i<size; i++){TreeNode temp = myqueue.poll();if(temp.left!=null){myqueue.add(temp.left);}if(temp.right!= null){myqueue.add(temp.right);}if(i == size-1){result.add(temp.val);}}}return result;}
}
637. 二叉树的层平均值 解法
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public List<Double> averageOfLevels(TreeNode root) {List<Double> result = new ArrayList<>();Queue<TreeNode> myqueue = new LinkedList<>();if(root == null) return result;myqueue.add(root);while(!myqueue.isEmpty()){int size = myqueue.size(); //得到size:当前层节点数double sum = 0;for(int i = 0; i<size; i++){ //弹出并得到size个节点的平均值,并将下一层的节点加入到队列TreeNode temp = myqueue.poll();sum += temp.val;if(temp.left != null){myqueue.add(temp.left);}if(temp.right != null){myqueue.add(temp.right);}}result.add(sum/size);}return result;}
}
429. N叉树的层序遍历
/*
// Definition for a Node.
class Node {public int val;public List<Node> children;public Node() {}public Node(int _val) {val = _val;}public Node(int _val, List<Node> _children) {val = _val;children = _children;}
};
*/class Solution {public List<List<Integer>> levelOrder(Node root) {List<List<Integer>> result = new ArrayList<>();Queue<Node> myqueue = new LinkedList<>();if(root == null) return result;myqueue.add(root);while(!myqueue.isEmpty()){int size = myqueue.size();List<Integer> restemp = new ArrayList<>();for(int i = 0; i < size; i++){Node temp = myqueue.poll();restemp.add(temp.val); // 将值temp.val加入restempfor(Node node:temp.children){ // 遍历temp.chirdren 即为遍历每一个temp的子节点,如果不为null就加入到列表中if(node != null){myqueue.add(node);}}}result.add(restemp);}return result;}
}
515. 在每个树行中找最大值
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> result = new ArrayList<>();Queue<TreeNode> myqueue = new LinkedList<>();if(root == null) return result;myqueue.add(root);while(!myqueue.isEmpty()){int size = myqueue.size();int max = (int)Math.pow(-2, 31);for(int i = 0; i < size; i++){TreeNode temp = myqueue.poll();if(temp.left != null){myqueue.add(temp.left);}if(temp.right != null){myqueue.add(temp.right);}if(max < temp.val){max = temp.val;}}result.add(max);}return result;}
}