import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return bool布尔型
*/
public boolean IsBalanced_Solution (TreeNode pRoot) {
// write code here
// 解题思路:
// 1.采用递归思想,先求出树及各子树的高度
// 2.如果左,右子树的高度差大于1则不是平衡二叉树
// 3.分别判断左,右子树是否是平衡二叉树
if (pRoot == null) {
return true;
}
int val = high(pRoot.left) - high(pRoot.right);
if (Math.abs(val) > 1) {
return false;
}
if (!IsBalanced_Solution(pRoot.left)) {
return false;
}
return IsBalanced_Solution(pRoot.right);
}
private int high(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(high(root.left), high(root.right)) + 1;
}
}