/**
* 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;
}
}
LINTCODE93:平衡二叉树
最新推荐文章于 2019-06-06 20:28:26 发布