My short O(n) recursive C++ code


  • 7
    J
    class Solution {
    public:
        int height(TreeNode *root)
        {
            if(root == NULL) return 0;
            int hl = height(root->left);
            int hr = height(root->right);
            if (hl == -1 || hr == -1 || abs(hl - hr) > 1) return -1;
            return max(hl, hr) + 1;
        }
        bool isBalanced(TreeNode *root) {
            return height(root) != -1;
        }
    };

  • 0
    O

    It's all the same to me ! haha


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.