码迷,mamicode.com
首页 > 其他好文 > 详细

110. Balanced Binary Tree

时间:2016-04-05 12:24:59      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * 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

标签:

原文地址:http://www.cnblogs.com/aguai1992/p/5354278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!