← Home

437. Path Sum III

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
437

This is one of those problems where the clean idea matters more than the amount of code. For 437. Path Sum III, the solution in this repository is mainly a two pointers solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are pathSumFrom, pathSum.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n^2) in worst case (no branching); O(nlogn) in best case (balanced tree).
  • Space: O(n) due to recursion.

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 28 ms, faster than 75.28% of C++ online submissions for Path Sum III.
02//Memory Usage: 14.8 MB, less than 100.00% of C++ online submissions for Path Sum III.
03
04https://leetcode.com/problems/path-sum-iii/discuss/91889/Simple-Java-DFS
05/**
06Typical recursive DFS.
07Space: O(n) due to recursion.
08Time: O(n^2) in worst case (no branching); O(nlogn) in best case (balanced tree).
09**/
10
11/**
12 * Definition for a binary tree node.
13 * struct TreeNode {
14 *     int val;
15 *     TreeNode *left;
16 *     TreeNode *right;
17 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
18 * };
19 */
20class Solution {
21public:
22    int pathSumFrom(TreeNode* root, int sum){
23        if(!root) return 0;
24        return (int)(root->val == sum) +
25            pathSumFrom(root->left, sum - root->val) + 
26            pathSumFrom(root->right, sum - root->val);
27    }
28    int pathSum(TreeNode* root, int sum) {
29        if(!root) return 0;
30        return pathSumFrom(root, sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
31    }
32};

Cost

Complexity

Time
O(n^2) in worst case (no branching); O(nlogn) in best case (balanced tree).
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) due to recursion.
Auxiliary state plus the answer structure where the problem requires one.