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.
My Solutions:
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
if (root.left == null && root.right == null) return targetSum == root.val;
return (hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val));
}
113.Path Sum II
Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values, not node references.
A root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children.
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: [[5,4,11,2],[5,8,4,5]]
Explanation: There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
public class Solution {
//recursive
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList();
if (root == null) return res;
dfs(root, sum, res, new ArrayList<Integer>());
return res;
}
private void dfs(TreeNode root, int target, List<List<Integer>> res, List<Integer> list) {
if (root == null) return;
list.add(root.val); // 记录路上的节点
// 代表走到了叶子节点,并且路径的值等于目标值
if (root.left == null && root.right == null && root.val == target) res.add(new ArrayList(list));
dfs(root.left, target - root.val, res, list);
dfs(root.right, target - root.val, res, list);
list.remove(list.size() - 1); // 注意这里删除了list里最后一个节点
}
}
//iterative
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
Stack<TreeNode> stack = new Stack<TreeNode>();
int SUM = 0;
TreeNode cur = root, pre = null;
while (cur != null || !stack.isEmpty()){
while(cur != null){
stack.push(cur);
path.add(cur.val);
SUM += cur.val;
cur = cur.left; // 先往左边走到底
}
cur = stack.peek();
if (cur.right != null && cur.right != pre){ // 往右边走到底
cur = cur.right;
continue;
}
// 代表走到了叶子节点,并且路径的值等于目标值
if (cur.left == null && cur.right == null && SUM == sum)
res.add(new ArrayList<Integer>(path));
pre = cur;
stack.pop();
// 这里和recursive的思路一样,因为这个节点不在路径上,需要删掉
path.remove(path.size() - 1);
SUM -= cur.val;
cur = null;
}
return res;
Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
Output: 3
Explanation: The paths that sum to 8 are shown.
public int pathSum(TreeNode root, int sum) {
if (root == null) return 0;
Map<Long, Integer> map = new HashMap<>();
// 用map储存 <prefix sum> --> <frequency>
map.put((long)0, 1);
return helper(root, (long)0, sum, map);
}
private int helper(TreeNode node, long total, int sum, Map<Long, Integer> map) {
if (node == null) return 0;
// update the prefix sum by adding the current node's value
total += node.val;
// get the number of valid path, ended by current node
int pathSumToCurrentNode = map.getOrDefault((long)total - sum, 0);
// update the map with current total
map.put((long)total, map.getOrDefault((long)total, 0) + 1);
int res = pathSumToCurrentNode
+ helper(node.left, total, sum, map)
+ helper(node.right, total, sum, map) ;
// restore the map
map.put((long)total, map.get(total) - 1);
return res;
}
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 node, int sum) {
if (node == null) return 0;
int res = 0;
if (node.val == sum) res++;
res += helper(node.left, sum - node.val) + helper(node.right, sum - node.val);
return res;
}