112. Path Sum
Problem
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
Solution Recursive
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) return false;
//has to be a root-to-leaf path
if (root.val == sum && root.left == null && root.right == null) return true;
return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val);
}
}
Update 2018-11
Solution Iterative
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) return false;
Deque<TreeNode> nodestack = new ArrayDeque<>();
Deque<Integer> sumstack = new ArrayDeque<>();
nodestack.push(root);
sumstack.push(sum);
while (!nodestack.isEmpty()) {
TreeNode curNode = nodestack.pop();
int curSum = sumstack.pop();
if (curNode.left == null && curNode.right == null && curNode.val == curSum) return true;
if (curNode.right != null) {
nodestack.push(curNode.right);
sumstack.push(curSum-curNode.val);
}
if (curNode.left != null) {
nodestack.push(curNode.left);
sumstack.push(curSum-curNode.val);
}
}
return false;
}
}
Path Sum II
Problem
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
Return:
[
[5,4,11,2],
[5,8,4,5]
]
Solution
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
dfs(root, sum, new ArrayList<Integer>(), res);
return res;
}
private void dfs(TreeNode root, int sum, List<Integer> temp, List<List<Integer>> res) {
if (sum == root.val && root.left == null && root.right == null) {
temp.add(root.val);
List<Integer> save = new ArrayList<>(temp); //important
res.add(save);
temp.remove(temp.size()-1);
} else {
temp.add(root.val);
if (root.left != null) dfs(root.left, sum-root.val, temp, res);
if (root.right != null) dfs(root.right, sum-root.val, temp, res);
temp.remove(temp.size()-1);
}
}
}
Path Sum III
Problem
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
Example:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1
Return 3. The paths that sum to 8 are:
- 5 -> 3
- 5 -> 2 -> 1
- -3 -> 11
Solution
class Solution {
public int pathSum(TreeNode root, int sum) {
if (root == null) return 0;
return helper(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
}
private int helper(TreeNode root, int sum) {
int count = 0;
if (sum == root.val) count++;
if (root.left != null) count += helper(root.left, sum-root.val);
if (root.right != null) count += helper(root.right, sum-root.val);
return count;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。