c++ recursive solution


  • 0
    B
    class Solution {
    public:
        bool hasPathSum(TreeNode* root, int sum) {
            if(!root) return false;
            sum-=root->val;
            if(!root->left && !root->right && sum==0) return true;
            
            return (hasPathSum(root->left, sum) || hasPathSum(root->right, sum)) ;
        }
    };
    

Log in to reply
 

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