Path Sum
LeetCode 112 | Difficulty: Easyβ
EasyProblem Descriptionβ
Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
A leaf is a node with no children.
Example 1:

Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.
Example 2:

Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There are two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.
Example 3:
Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.
Constraints:
- The number of nodes in the tree is in the range `[0, 5000]`.
- `-1000 <= Node.val <= 1000`
- `-1000 <= targetSum <= 1000`
Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Approachβ
Tree DFSβ
Traverse the tree recursively (or with a stack). At each node, decide: what information do I need from the left/right subtrees? Process: go left β go right β combine results. Consider preorder, inorder, or postorder traversal based on when you need to process the node.
Path problems, subtree properties, tree structure manipulation.
Tree BFS (Level-Order)β
Use a queue to process the tree level by level. At each level, process all nodes in the queue, then add their children. Track the level size to know when one level ends and the next begins.
Level-order traversal, level-based aggregation, right/left side view.
Solutionsβ
Solution 1: C# (Best: 112 ms)β
| Metric | Value |
|---|---|
| Runtime | 112 ms |
| Memory | N/A |
| Date | 2018-05-03 |
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public bool HasPathSum(TreeNode root, int sum) {
Stack<TreeNode> nodes = new Stack<TreeNode>();
Stack<int> sums = new Stack<int>();
nodes.Push(root);
sums.Push(sum);
while(nodes.Count != 0 && root!=null)
{
var top = nodes.Pop();
int value = sums.Pop();
if(top.left==null && top.right==null && top.val == value)
{
return true;
}
if(top.right!=null)
{
nodes.Push(top.right);
sums.Push(value-top.val);
}
if (top.left != null)
{
nodes.Push(top.left);
sums.Push(value - top.val);
}
}
return false;
}
}
π 2 more C# submission(s)
Submission (2018-04-10) β 176 ms, N/Aβ
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public bool HasPathSum(TreeNode root, int sum) {
if(root==null) return false;
if(root.left == null && root.right == null) return sum == root.val;
return HasPathSum(root.left, sum-root.val) || HasPathSum(root.right, sum-root.val);
}
}
Submission (2018-04-10) β 180 ms, N/Aβ
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public bool HasPathSum(TreeNode root, int sum) {
Stack<TreeNode> nodes = new Stack<TreeNode>();
Stack<int> sums = new Stack<int>();
nodes.Push(root);
sums.Push(sum);
while(nodes.Count != 0 && root!=null)
{
var top = nodes.Pop();
int value = sums.Pop();
if(top.left==null && top.right==null && top.val == value)
{
return true;
}
if(top.right!=null)
{
nodes.Push(top.right);
sums.Push(value-top.val);
}
if (top.left != null)
{
nodes.Push(top.left);
sums.Push(value - top.val);
}
}
return false;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Tree Traversal | $O(n)$ | $O(h)$ |
Interview Tipsβ
- Start by clarifying edge cases: empty input, single element, all duplicates.
- Consider: "What information do I need from each subtree?" β this defines your recursive return value.