搜索:
剑指 Offer 55 - I. 二叉树的深度
class Solution {
int max=0;
public int maxDepth(TreeNode root) {
max=DFS(root,0);
return max;
}
public int DFS(TreeNode node,int len){
if(node==null){
return len ;
}
return Math.max(DFS(node.left,len+1),DFS(node.right,len+1));
}
}
110. 平衡二叉树
关键条件:Math.abs(left-right)<=1
class Solution {
public boolean isBalanced(TreeNode root) {
return DFS(root)>=0;
}
public int DFS(TreeNode node){
if(node==null){
return 0;
}
int left= DFS(node.left);
int right=DFS(node.right);
if(left>=0 && right>=0 && Math.abs(left-right)<=1){
return Math.max(left,right)+1;
}else{
return -1;
}
}
}
排序:
网友评论