平衡二叉树

题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root==null){
            return true;
        }
         if(Math.abs(TreeHeight(root.left) - TreeHeight(root.right)) <= 1) {
             return IsBalanced_Solution(root.left)&&IsBalanced_Solutio(root.right);
         }else{
        return false;
         }
    }
     public int TreeHeight(TreeNode root){
        if(root == null) {
            return 0;
        }
        return Math.max(TreeHeight(root.left),
                        TreeHeight(root.right)) + 1;
    }
}