首页 > 其他 > 详细

剑指offer-平衡二叉树

时间:2020-03-25 22:23:27      阅读:60      评论:0      收藏:0      [点我收藏+]

题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root==null)
            return true;
        int depthleft = TreeDepth(root.left);
        int depthright = TreeDepth(root.right);
        int cw = depthleft-depthright;
        if(cw<-1 || cw>1) return false;
        return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);
            
        
        
    }
    public int TreeDepth(TreeNode root) {
        if(root==null)
            return 0;
        int nleft = TreeDepth(root.left);
        int nright = TreeDepth(root.right);
        return Math.max(nleft,nright)+1;
    } 
}

 

剑指offer-平衡二叉树

原文:https://www.cnblogs.com/loyolh/p/12569304.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!