平衡二叉树

0 阅读1分钟
package org.example.leetcodeBook.deepSearch;  
  
// 平衡二叉树  
public class IsBalancedSolution {  
    public boolean isBalanced(InorderTraversalSolution.TreeNode root){  
        if(root==null)return true;  
        else return Math.abs(height(root.left) - height(root.right))<=1 &&  
            isBalanced(root.left) && isBalanced(root.right);  
    }  
    public int height(InorderTraversalSolution.TreeNode root){  
        if(root==null) return 0;  
        else return Math.max(height(root.left),height(root.right))+1;  
    }  
}