Simplest C++ Recursive One-Liner


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