题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
源代码:递归解法
public class Solution {
public int max(int a,int b){
return a>b?a:b;
}
public boolean IsBalanced_Solution(TreeNode root) {
if(root==null) return true;
//如果根的左右子树的深度的绝对值不大于1并且左子树是平衡树并且右子树是平衡树,则说明这棵树是平衡树
return (Math.abs(maxdeep(root.left)-maxdeep(root.right))<=1)&&IsBalanced_Solution(root.left)
&&IsBalanced_Solution(root.right);
}
public int maxdeep(TreeNode root){//获取树的最大深度
if(root==null) return 0;
return max(1+maxdeep(root.left),1+maxdeep(root.right));
}
}
1万+

被折叠的 条评论
为什么被折叠?



