LeetCode 110. Balanced Binary Tree

1. 题目描述

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.

2. 解题思路

这是一道非常easy 的题目, 可以使用递归的思路进行处理, 记录下左右子树的最大高度, 以及他们是否平衡的信息即可。
三个月前的代码中, 我们单独写了一个统计高度的函数, 感觉非常冗余, 于是这次就把统计高度和判断平衡的代码整合到了一起。

3. code

// 三个月前的代码 201601xx
class Solution {
public:
    int calcHeight(TreeNode * root){
        if (root == nullptr)
            return 0;

        int left = calcHeight(root->left);
        int right = calcHeight(root->right);
        return left > right ? left + 1 : right + 1;
    }

    bool isBalanced(TreeNode* root) {
        if (root == nullptr)
            return true;

        int left = calcHeight(root->left);
        int right = calcHeight(root->right);
        if (abs(left - right) > 1)
            return false;

        return isBalanced(root->left) && isBalanced(root->right);
    }
};
// 20160406
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        int maxheight = 0;
        return _isBalanced(root, maxheight);
    }

private:
    bool _isBalanced(TreeNode * root, int & maxheight){
        if (root == nullptr){
            return true;
        }
        int left = 0, right = 0;
        bool ret = _isBalanced(root->left, left) && _isBalanced(root->right, right) && abs(left - right) <= 1;
        maxheight += max(left, right) + 1;
        return ret;
    }
};

4. 大神代码

感觉, 其实他所说的DFS 的方法, 应该就是我们这里的第二种方法了。。。。

This problem is generally believed to have two solutions: the top down approach and the bottom up way.

1.The first method checks whether the tree is balanced strictly according to the definition of balanced binary tree: the difference between the heights of the two sub trees are not bigger than 1, and both the left sub tree and right sub tree are also balanced. With the helper function depth(), we could easily write the code;

class solution {
public:
    int depth (TreeNode *root) {
        if (root == NULL) return 0;
 return max (depth(root -> left), depth (root -> right)) + 1;
    }

    bool isBalanced (TreeNode *root) {
        if (root == NULL) return true;

        int left=depth(root->left);
        int right=depth(root->right);
 return abs(left - right) <= 1 && isBalanced(root->left) && isBalanced(root->right);
    }
};
For the current node root, calling depth() for its left and right children actually has to access all of its children, thus the complexity is O(N). We do this for each node in the tree, so the overall complexity of isBalanced will be O(N^2). This is a top down approach.

2.The second method is based on DFS. Instead of calling depth() explicitly for each child node, we return the height of the current node in DFS recursion. When the sub tree of the current node (inclusive) is balanced, the function dfsHeight() returns a non-negative value as the height. Otherwise -1 is returned. According to the leftHeight and rightHeight of the two children, the parent node could check if the sub tree is balanced, and decides its return value.

class solution {
public:
int dfsHeight (TreeNode *root) {
        if (root == NULL) return 0;

        int leftHeight = dfsHeight (root -> left);
        if (leftHeight == -1) return -1;
        int rightHeight = dfsHeight (root -> right);
        if (rightHeight == -1) return -1;

        if (abs(leftHeight - rightHeight) > 1)  return -1;
 return max (leftHeight, rightHeight) + 1;
    }
    bool isBalanced(TreeNode *root) {
 return dfsHeight (root) != -1;
    }
};
In this bottom up approach, each node in the tree only need to be accessed once. Thus the time complexity is O(N), better than the first solution.

你可能感兴趣的:(LeetCode)