题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if (root == null) {
return true;
}
if (Math.abs(this.TreeDepth(root.left)-this.TreeDepth(root.right))<=1) {
return true;
}
else {
return false;
}
}
public int TreeDepth(TreeNode root) {
if (root == null) {
return 0;
}
int depth;
if (this.TreeDepth(root.left) > this.TreeDepth(root.right)) {
return 1 + this.TreeDepth(root.left);
}
else {
return 1 + this.TreeDepth(root.right);
}
}
}