2 lines only - recursive - very concise


  • 0
     bool hasPathSum(TreeNode* root, int sum) {
            if(root && root->left==NULL && root->right==NULL && sum-root->val == 0) return true;
            return root && ((root->left && hasPathSum(root->left, sum-root->val)) || (root->right && hasPathSum(root->right, sum-root->val)));
        }

Log in to reply
 

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