美文网首页
39、平衡二叉树

39、平衡二叉树

作者: quiterr | 来源:发表于2017-09-02 21:46 被阅读0次

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

    public class Solution {
        public boolean IsBalanced_Solution(TreeNode root) {
            if(root!=null){
                int left = TreeDepth(root.left);
                int right = TreeDepth(root.right);
                if(left-right>1||right-left>1){
                    return false;
                }else{
                    boolean leftB = IsBalanced_Solution(root.left);
                    boolean rightB = IsBalanced_Solution(root.right);
                    return leftB&&rightB;
                }
            }
            return true;
        }
        
        public int TreeDepth(TreeNode root) {
            if(root!=null){
                int left = TreeDepth(root.left);
                int right = TreeDepth(root.right);
                if(left>=right){
                    return left+1;
                }else{
                    return right+1;
                }
            }
            
            return 0;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:39、平衡二叉树

          本文链接:https://www.haomeiwen.com/subject/tlsuwttx.html