Have a question about a basic java sample for binary tree: Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target.(A valid path is from root node to any of the leaf nodes.).
Why we need to path.remove(path.size() - 1);
when it go through the left, right node?
Here is the code sample:
public class Solution {
public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) {
return result;
}
ArrayList<Integer> path = new ArrayList<Integer>();
path.add(root.val);
helper(root, path, root.val, target, result);
return result;
}
private void helper(TreeNode root,
ArrayList<Integer> path,
int sum,
int target,
List<List<Integer>> result) {
// meet leaf
if (root.left == null && root.right == null) {
if (sum == target) {
result.add(new ArrayList<Integer>(path));
}
return;
}
// go left
if (root.left != null) {
path.add(root.left.val);
helper(root.left, path, sum + root.left.val, target, result);
path.remove(path.size() - 1);
}
// go right
if (root.right != null) {
path.add(root.right.val);
helper(root.right, path, sum + root.right.val, target, result);
path.remove(path.size() - 1);
}
}
}