[LeetCode 110] Balanced Binary Tree (Easy)

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

  • a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
   / \
  9  20
    /  \
   15   7
Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
      / \
     2   2
    / \
   3   3
  / \
 4   4
Return false.
[LeetCode 110] Balanced Binary Tree (Easy)_第1张图片
image.png

SOLUTION

[LeetCode 110] Balanced Binary Tree (Easy)_第2张图片
image.png

Solution1

  1. 根据平衡二叉树的定义,需要满足
    左右子树的高度差<=1 && 左子树是平衡二叉树 && 右子树是平衡二叉树
    为什么需要判断左右子树是否平衡的? 反例见上图。
  2. 二叉树高度定义:


    image.png

缺点:每次求左右子树的高度时,已经遍历了左右所有节点。但是当递归求左右子树是否是平衡时,又会再次分别递归访问左右子树。所以Worst case Time Complexity: O(n) + 2*O(n/2) + 4*O(n/4) + ... = O (nlogn)

Solution2
优化

  1. 用一个Global boolean variable 来记录当前subtree是否是平衡的,初始化为true。
  2. 每次在求左右高度的时候就求出是否平衡,这种一旦发现不平衡,设置global variable 为false,然后直接退出。
  3. 每个节点只访问一次,不会重复。 O(n)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    /***************** Solution 1 O(nlogn) ******************/
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        
        int leftHeight = height (root.left);
        int rightHeight = height (root.right);
        
        return (Math.abs (leftHeight - rightHeight) <= 1) && isBalanced (root.left) && isBalanced (root.right);
    }
    
    private int height (TreeNode root) {
        if (root == null) {
            return 0;
        }
        
        int leftHeight = height (root.left);
        int rightHeight = height (root.right);
        
        return Math.max (leftHeight, rightHeight) + 1;
    }
    /***************** Solution 1 end ******************/
    //////////////////////////////////////////////////////////////////
    /***************** Solution 2 O(n) BETTER!!!!!******************/
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        
        boolean[] currentIsBalanced = { true };
        
        isBalancedHelper (root, currentIsBalanced);
        
        return currentIsBalanced [0];
    }
    
    private int isBalancedHelper (TreeNode root, boolean[] currentIsBalanced) {
        if (root == null) {
            return 0;
        }
        
        int leftHeight = isBalancedHelper (root.left, currentIsBalanced);
        int rightHeight = isBalancedHelper (root.right, currentIsBalanced);
        
        if (Math.abs (leftHeight - rightHeight) > 1) {
            currentIsBalanced [0] = false;
            return -1;
        }
        
        return Math.max (leftHeight, rightHeight) + 1;
    }
    /***************** Solution 2 end ******************/
}

你可能感兴趣的:([LeetCode 110] Balanced Binary Tree (Easy))