剑指offer-判断该二叉树是否是平衡二叉树

来源:互联网 发布:淘宝买iphone4s 编辑:程序博客网 时间:2024/06/13 05:43
public class Solution {    public boolean IsBalanced_Solution(TreeNode root) {        if(root==null)            return true;        int l=TreeDepth(root.left);        int r=TreeDepth(root.right);        int temp=l-r;        if(temp>1||temp<-1)//根据平衡二叉树的性质,左右两个子树的高度差的绝对值不超过1            return false;        return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);    }    //求树的深度    public int TreeDepth(TreeNode root){        if(root==null)            return 0;        int left=1;        int right=1;        left=left+TreeDepth(root.left);        right=right+TreeDepth(root.right);        return left>right?left:right;    }}
阅读全文
0 0
原创粉丝点击