Share my 2 lines recursive C++ solution(12ms)

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

  • 0

    I don't undertand if there exist several leaf node.just one line equals sum,another paths don't equal sum.Why the result is true not false?

Log in to reply

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