zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    numpy 矩阵和数组
    python map()
    python matplotlib plot
    python mean()
    预测数值型数据:回归
    散点图
    非均衡分类问题
    AdaBoost元算法
    2.1 n元排列
    1.3 数域
  • 原文地址:https://www.cnblogs.com/aguai1992/p/5354278.html
Copyright © 2011-2022 走看看