标签:
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
boolean result = false;
if (root == null) {
return true;
}
if (isBalanceTree(root) > 0) {
result = true;
}
return result;
}
public int isBalanceTree(TreeNode root) {
if (root == null) {
return 0;
}
int left = isBalanceTree(root.left);
int right = isBalanceTree(root.right);
if (left >= 0 && right >= 0) {
int diff = left - right;
if (diff >= -1 && diff <= 1) {
int result = 1 + ((left > right) ? left: right);
return result;
}
}
return -1;
}
}
标签:
原文地址:http://www.cnblogs.com/rosending/p/5657262.html