LINTCODE93:平衡二叉树

来源:互联网 发布:网络兼职怎么找 编辑:程序博客网 时间:2024/06/05 09:00
/** * Definition of TreeNode: * public class TreeNode { *     public int val; *     public TreeNode left, right; *     public TreeNode(int val) { *         this.val = val; *         this.left = this.right = null; *     } * } */public class Solution {    /*     * @param root: The root of binary tree.     * @return: True if this Binary tree is Balanced, or false.     */    public boolean isBalanced(TreeNode root) {        // write your code here        if(root==null){            return true;        }        boolean falg = false;        int dep = Math.abs(getHigh(root.left)-getHigh(root.right));        if(dep< 2&& isBalanced(root.left) && isBalanced(root.right)){            return true;        }else{            return false;        }    }    public int getHigh(TreeNode root){        if(root==null){            return 0;        }        int right = getHigh(root.right);        int left = getHigh(root.left);        int temp = right>=left?right:left;        return temp+1;    }}