17 ms c++ solution


  • 1
    P
    class Solution {
    public:
        bool isBalanced(TreeNode *root) {
            int depth = 0;
            return helper(root, depth);
        }
        
        bool helper(TreeNode* root, int &depth) {
            if (root == NULL) return true;
            int l = depth+1;
            int r = depth+1;
            if (!helper(root->left, l) || !helper(root->right, r) || abs(l-r) > 1) {
                return false;
            }
            depth = max(l,r);
            return true;
        }
    };

Log in to reply
 

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