判断一棵树是不是二叉平衡树
转载代码:
public class Solution {
private boolean isBalanced = false;//最后的返回值
public boolean IsBalanced_Solution(TreeNode root) {
getDepth(root);
return isBalanced;
}
public int getDepth(TreeNode root) {
if(root == null) {
isBalanced = true;
return 0;
}
int left = getDepth(root.left);//递归左子树
int right = getDepth(root.right);//递归右子树
int depth = (left > right ? left : right) + 1;
if(Math.abs(left - right) <= 1) {
isBalanced = true;
} else {
isBalanced = false;
}
return depth;
}
}
转载地址:https://blog.csdn.net/endif6/article/details/82356256
还没有评论,来说两句吧...