目录
1、116.填充每个节点的下一个右侧节点指针
easy
class Solution {
public Node connect(Node root) {
if (root == null)
return root;
Queue<Node> que = new LinkedList<>();
List<Integer> res = new ArrayList<>();
que.add(root);
while(!que.isEmpty())
{
int len = que.size();
Node t;
Node cur;
cur = que.poll();
if (cur.left != null)
que.add(cur.left);
if (cur.right != null)
que.add(cur.right);
for(int i = 1; i < len; ++i)
{
t = que.poll();
if (t.left != null)
que.add(t.left);
if (t.right != null)
que.add(t.right);
cur.next = t;
cur = t;
}
}
return root;
}
}
2、117.填充每个节点的下一个右侧节点指针II
和上一题没差啦~就一样的
3、104.二叉树的最大深度
easy
class Solution {
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
Queue<TreeNode> que = new LinkedList<>();
int res = 0;
que.add(root);
while(!que.isEmpty())
{
int len = que.size();
for(int i = 0; i < len; ++i)
{
TreeNode t = que.poll();
if (t.left != null)
que.add(t.left);
if (t.right != null)
que.add(t.right);
}
++res;
}
return res;
}
}
4、111.二叉树的最小深度
可以用上题改改,也可以用递归。
递归时需注意,当有一个孩子为空时,最小深度为另一个孩子的最小深度加1.
class Solution {
public int minDepth(TreeNode root) {
if (root == null)
return 0;
if (root.left == null || root.right == null)
return minDepth(root.left) + minDepth(root.right) + 1;
return 1 + Math.min(minDepth(root.left),minDepth(root.right));
}
}
5、翻转二叉树
递归
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null)
return root;
TreeNode t;
t = root.left;
root.left = root.right;
root.right = t;
if (root.left != null) invertTree(root.left);
if (root.right != null) invertTree(root.right);
return root;
}
}
6、101.对称二叉树
递归
class Solution {
public boolean isSymmetric(TreeNode root) {
return compare(root.left,root.right);
}
public boolean compare(TreeNode t1, TreeNode t2)
{
if(t1 == null && t2 != null) return false;
if(t1 != null && t2 == null) return false;
if(t1 == null && t2 == null) return true;
if(t1.val != t2.val) return false;
return compare(t1.left,t2.right) && compare(t1.right,t2.left);
}
}
7、100.相同的树
和上一题差不多
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null) return true;
else if(p != null && q == null) return false;
else if(p == null && q != null) return false;
else if(p.val != q.val) return false;
else return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
}
8、另一棵树的子树
递归
三种情况:(1)两树相同(2)子树在根的左子树(3)子树在根的右子树
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if(isSameTree(root,subRoot)) return true;
Boolean isLeft = false, isRight = false;
if(root.left != null) isLeft = isSubtree(root.left,subRoot);
if(root.right != null) isRight = isSubtree(root.right,subRoot);
return isLeft || isRight;
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null) return true;
else if(p != null && q == null) return false;
else if(p == null && q != null) return false;
else if(p.val != q.val) return false;
else return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
}
9、N叉树的最大深度
递归
class Solution {
public int maxDepth(Node root) {
if (root == null) return 0;
int len = root.children.size();
int max = 0;
while(len > 0)
{
max = Math.max(max,maxDepth(root.children.get(len - 1)));
--len;
}
return 1 + max;
}
}