//JAVA
//这题只考虑平衡性(即 左右子树的深度差不大于1)
//不考虑平很二叉树的搜索性(中序排列是有序列表,父亲节点的值大于左孩子节点的值,小于右孩子节点的值)
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
return depth(root) != -1;
}
public int depth(TreeNode root){
//必要条件
if(root == null) return 0;
int left = depth(root.left);
if(left == -1) return -1;
int right = depth(root.right);
if(right == -1) return -1;
//深度大于一,不是平衡二叉树
if(left -right <(-1) || left -right >1)
return -1;
else
return 1 + (left >right ? left:right);
}
}


京公网安备 11010502036488号