Algo-Sim › Binary Trees & BST
Path Sum
Easy · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Subtract each node from the target on the way down; a path works if a leaf leaves exactly 0.
Problem
Given the root of a binary tree and targetSum, return true if some path from the root down to a leaf has values that add up to targetSum. A leaf is a node with no children.
Constraints
- 0 ≤ number of nodes ≤ 5000
- -1000 ≤ Node.val, targetSum ≤ 1000
- Values may be negative, so you cannot stop early when the sum overshoots
Edge cases to test
- Empty tree:
falseeven for target 0 - A non-leaf node that hits the target (doesn’t count)
- Negative values
Follow-up questions
- What if paths can start and end anywhere?
- Prefix sums along the root path in a hash map (Path Sum III): O(n).
- How do you return every path?
- Backtrack with one path list (Path Sum II).
Solution
Java
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
int remaining = targetSum - root.val;
if (root.left == null && root.right == null) return remaining == 0;
if (hasPathSum(root.left, remaining)) return true;
return hasPathSum(root.right, remaining);
}Python
def has_path_sum(root, target_sum):
if not root:
return False
remaining = target_sum - root.val
if not root.left and not root.right:
return remaining == 0
if has_path_sum(root.left, remaining):
return True
return has_path_sum(root.right, remaining)C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool hasPathSum(TreeNode *root, int targetSum) {
if (root == nullptr)
return false;
int remaining = targetSum - root->val;
if (root->left == nullptr && root->right == nullptr)
return remaining == 0;
if (hasPathSum(root->left, remaining))
return true;
return hasPathSum(root->right, remaining);
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)