Simple C++ Recursive Solution


  • 0
    M
    class Solution {
    public:
        pair<int, bool> SolveRec(TreeNode const * root) {
            if (!root) return make_pair(0, true);
            auto l = SolveRec(root->left), r = SolveRec(root->right);
            return make_pair(max(l.first, r.first) + 1, l.second && r.second && abs(l.first - r.first) <= 1);
        }
        bool isBalanced(TreeNode* root) {
            return SolveRec(root).second;
        }
    };
    

Log in to reply
 

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