题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树
package com.hk;
/**
* @author hk
* @version 1.0
* @date 2020/4/2 0002 15:09
*/
public class Solution {
public int TreeDepth(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return Math.max(TreeDepth(root.left), TreeDepth(root.right)) + 1;
}
public boolean IsBalanced_Solution(TreeNode root) {
if (root == null) {
return true;
}
if (Math.abs(TreeDepth(root.left) - TreeDepth(root.right)) > 1) {
return false;
}
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
}