LeetCode刷题笔记(110,平衡二叉树,Easy)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null){
            return true;
        }
        return (Math.abs(MaxDepth(root.left)-MaxDepth(root.right)) <= 1) && isBalanced(root.left) && isBalanced(root.right);

    }
    public int MaxDepth(TreeNode root){
        if(root == null){
            return 0;
        }
        int lefthigh = MaxDepth(root.left);
        int righthigh = MaxDepth(root.right);
        return 1 + Math.max(lefthigh,righthigh);
    }
}

 

你可能感兴趣的:(LeetCode刷题笔记(110,平衡二叉树,Easy))