您的位置:首页 > 其它

平衡二叉树

2015-10-11 19:53 253 查看
输入一棵二叉树,判断该二叉树是否是平衡二叉树。

package balancedTree;

class TreeNode{
TreeNode left;
TreeNode right;
int val;
}

public class Solution {

public boolean IsBalanced_Solution(TreeNode root) {
if(root==null){
return true;
}
if(Math.abs(treeDepth(root.left)-treeDepth(root.right))<=1){
return true;
}else{
return false;
}
}

public int treeDepth(TreeNode root){
if(root==null){
return 0;
}
int i=treeDepth(root.left);
int j=treeDepth(root.right);
int depth=(i>j)?(i+1):(j+1);
return depth;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: