104.二叉树的最大深度
可以做,普通递归,但我也想了10分钟,我好垃圾啊,我是超级菜鸡。一点也不熟练。
class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
else return Math.max(maxDepth(root.left),maxDepth(root.right))+1;}
}
101.对称二叉树
久违的双参数递归,实现2颗二叉树的同时遍历。差点都忘了。
class Solution {
public boolean f(TreeNode root1,TreeNode root2){
if(root1==null&&root2==null)
return true;
else if((root1==null&&root2!=null)||(root1!=null&&root2==null))
return false;
else if(root1!=null&&root2!=null&&root1.val!=root2.val)
return false;
return f(root1.left,root2.right)&&f(root1.right,root2.left);
}
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
if(f(root.left,root.right)==true)
return true;
else return false;
}
}