二叉树的层序遍历:10题
Queue
if (root != null) queue.add(root);
while (!queue.isEmpty())
int size = queue.size();
List
while (size-- > 1)
node = queue.pop();
list.add(node.val);
if (node.left != null ) queue.add(node.left);
if (node.right = null) queue.add(node.right);
res.add(list)
/**
* 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> levelOrder(TreeNode root) {
List> res = new ArrayList<>();
Queue queue = new LinkedList<>();
if (root != null) queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List list = new ArrayList<>();
while (size-- > 0) {
TreeNode cur = queue.poll();
list.add(cur.val);
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
res.add(new ArrayList<>(list));
}
return res;
}
}
Binary Tree Level Order Traversal II - LeetCode
把最后结果倒排,直接用add,把每一个新的list加到res的最前面
/**
* 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> levelOrderBottom(TreeNode root) {
List> res = new ArrayList<>();
Queue queue = new LinkedList<>();
if (root == null) return res;
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List list = new ArrayList<>();
while (size-- > 0) {
TreeNode cur = queue.poll();
list.add(cur.val);
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
res.add(0, list);
}
return res;
}
}
Binary Tree Right Side View - LeetCode
这个就不用while了,因为要取每个level的最后一个
/**
* 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 rightSideView(TreeNode root) {
List res = new ArrayList<>();
Queue queue = new LinkedList<>();
if (root == null) return res;
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
if (i == size - 1) {
res.add(cur.val);
}
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
}
return res;
}
}
Average of Levels in Binary Tree - LeetCode
/**
* 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 averageOfLevels(TreeNode root) {
List res = new ArrayList<>();
if (root == null) return res;
Queue queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
double sum = 0;
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
sum += cur.val;
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
res.add(sum / size);
}
return res;
}
}
N-ary Tree Level Order Traversal - LeetCode
/*
// Definition for a Node.
class Node {
public int val;
public List children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List> levelOrder(Node root) {
List> res = new ArrayList<>();
if (root == null) return res;
Queue queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List list = new ArrayList<>();
for (int i = 0; i < size; i++) {
Node cur = queue.poll();
list.add(cur.val);
List children = cur.children;
if (children == null || children.size() == 0) continue;
for (Node child : children) {
if (child != null) {
queue.add(child);
}
}
}
res.add(list);
}
return res;
}
}
Find Largest Value in Each Tree Row - LeetCode
/**
* 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 largestValues(TreeNode root) {
List res = new ArrayList<>();
if (root == null) return res;
Queue queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
max = Math.max(cur.val, max);
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
res.add(max);
}
return res;
}
}
Populating Next Right Pointers in Each Node - LeetCode
这个解法真是太精妙了 每一层里面,i < size - 1, 把当前node指向queue里的第一个
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
if (root == null) return root;
Queue queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Node cur = queue.poll();
if (i < size - 1) {
cur.next = queue.peek();
}
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
}
return root;
}
}
解法和上一题没有任何区别
这道题目说是二叉树,但116题目说是完整二叉树,其实没有任何差别,一样的代码一样的逻辑一样的味道
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
Queue queue = new LinkedList<>();
if (root == null) return null;
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Node cur = queue.poll();
if (i < size - 1) {
cur.next = queue.peek();
}
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
}
return root;
}
}
Maximum Depth of Binary Tree - LeetCode
其实就是求有多少层,二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
/**
* 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 int maxDepth(TreeNode root) {
if (root == null) return 0;
Queue queue = new LinkedList<>();
int level = 0;
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
level++;
}
return level;
}
}
111. 二叉树的最小深度 - 力扣(Leetcode)
相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。
需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点
起始depth从1开始,因为就算root为空,也是有一层
/**
* 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 int minDepth(TreeNode root) {
if (root == null) return 0;
Queue queue = new LinkedList<>();
queue.add(root);
int depth = 1;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
if (cur.left == null && cur.right == null) {
return depth;
}
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
depth++;
}
return depth;
}
}
226.翻转二叉树 Invert Binary Tree - LeetCode
可以前序遍历,也可以后序遍历,中序遍历要注意swap之后遍历的是同一边
/**
* 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 TreeNode invertTree(TreeNode root) {
if (root == null) return root;
swap(root);
invertTree(root.left);
invertTree(root.right);
return root;
}
private void swap(TreeNode root) {
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
}
}
/**
* 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 boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return dfs(root.left, root.right);
}
private boolean dfs(TreeNode left, TreeNode right) {
if (left == null && right == null) return true;
if (left == null || right == null) return false;
return left.val == right.val && dfs(left.left, right.right) && dfs(left.right, right.left);
}
}