public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if (root != null) {
int left = deep(root.left);
int right = deep(root.right);
if (Math.abs(left - right) > 1) {
return false;
} else {
return IsBalanced_Solution(root.left) &&
IsBalanced_Solution(root.right);
}
}
return true;
}
// 求树的深度
public int deep(TreeNode node) {
if (node == null) {
return 0;
}
return Math.max(deep(node.left), deep(node.right)) + 1;
}
}
解题思想:主要理解平衡二叉树的性质:平衡⼆叉树(Balanced Binary Tree),具有以下性质:它是⼀棵空树或它的左右两个
⼦树的⾼度差的绝对值不超过1,并且左右两个⼦树都是⼀棵平衡⼆叉树。

京公网安备 11010502036488号