110. Balanced Binary Tree

Posted 阿怪123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了110. Balanced Binary Tree相关的知识,希望对你有一定的参考价值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isBalanced(TreeNode root) {
        Flag f=new Flag();
        int depth=getDepth(root,f);
        return f.getFlag();
        
    }
    public class Flag{
        private boolean flag;
        public Flag()
        {
            this.flag=true;
        }
        public Flag(boolean flag)
        {
            this.flag=flag;
        }
        public void setTrue()
        {
            this.flag=true;
        }
        public void setFalse()
        {
            this.flag=false;
        }
        public boolean getFlag()
        {
            return this.flag;
        }
        
    }

    int getDepth(TreeNode temp,Flag f)
    {
        if(temp==null)
            return 0;
        int leftDepth=getDepth(temp.left,f);
        int rightDepth=getDepth(temp.right,f);
        if(((int)Math.abs(leftDepth-rightDepth))<=1&&f.getFlag()==true&&f.getFlag()==true)
            f.setTrue();
        else
            f.setFalse();
        return max(leftDepth,rightDepth)+1;
    }
    int max(int a,int b)
    {
        return a>b?a:b;
    }
}

 

以上是关于110. Balanced Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode[110]-Balanced Binary Tree

LC.110. Balanced Binary Tree

110. Balanced Binary Tree

110. Balanced Binary Tree

110. Balanced Binary Tree

110. Balanced Binary Tree 110.平衡二叉树