[Path Sum][C++] - 1 Line Solution


  • 0
    class Solution {
    public:
        bool hasPathSum(TreeNode* root, int sum) {
            return !root ? false : (!root->left && !root->right && root->val == sum) || (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.