c++,use val save depth


  • 0
    Q
    class Solution {
    public:
        bool isBalanced(TreeNode* root) {
            if(!root)
            {
                return true;
            }
            bool lr = isBalanced(root->left) && isBalanced(root->right);
            if (!lr)
            {
    			return false;
            }
    		int left = root->left ? root->left->val : 0;
    		int right = root->right ? root->right->val : 0;
    		root->val = (left > right ? left : right) + 1;
    		int sub = left - right;
    		return sub >= -1 && sub <= 1;
        }
    };
    

Log in to reply
 

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