|
| 1 | +package medium; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import utils.CommonUtils; |
| 7 | +import classes.TreeNode; |
| 8 | + |
| 9 | +/**113. Path Sum II QuestionEditorial Solution My Submissions |
| 10 | +Total Accepted: 90131 |
| 11 | +Total Submissions: 306937 |
| 12 | +Difficulty: Medium |
| 13 | +Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. |
| 14 | +
|
| 15 | +For example: |
| 16 | +Given the below binary tree and sum = 22, |
| 17 | + 5 |
| 18 | + / \ |
| 19 | + 4 8 |
| 20 | + / / \ |
| 21 | + 11 13 4 |
| 22 | + / \ / \ |
| 23 | + 7 2 5 1 |
| 24 | +return |
| 25 | +[ |
| 26 | + [5,4,11,2], |
| 27 | + [5,8,4,5] |
| 28 | +] |
| 29 | +*/ |
| 30 | +public class PathSumII { |
| 31 | + //also, it's possible that a node's value could be negative, as long as the sum of root to leaf ends up to sum |
| 32 | + public List<List<Integer>> pathSum(TreeNode root, int sum) { |
| 33 | + List<List<Integer>> allPaths = new ArrayList(); |
| 34 | + if(root == null) return allPaths; |
| 35 | + List<Integer> path = new ArrayList(); |
| 36 | + dfs(root, path, allPaths, sum); |
| 37 | + return allPaths; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + private void dfs(TreeNode root, List<Integer> path, List<List<Integer>> allPaths, int sum) { |
| 42 | + path.add(root.val); |
| 43 | + if(root.left != null){ |
| 44 | + dfs(root.left, path, allPaths, sum-root.val); |
| 45 | + } |
| 46 | + if(root.right != null){ |
| 47 | + dfs(root.right, path, allPaths, sum-root.val); |
| 48 | + } |
| 49 | + if(root.left == null && root.right == null){ |
| 50 | + if(sum == root.val){ |
| 51 | + List<Integer> onePath = new ArrayList(path); |
| 52 | + allPaths.add(onePath); |
| 53 | + } |
| 54 | + } |
| 55 | + path.remove(path.size()-1); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + public static void main(String...strings){ |
| 60 | + PathSumII test = new PathSumII(); |
| 61 | +// TreeNode root = new TreeNode(1); |
| 62 | +// root.left = new TreeNode(2); |
| 63 | +// int sum = 1; |
| 64 | + |
| 65 | +// TreeNode root = new TreeNode(1); |
| 66 | +// root.left = new TreeNode(-2); |
| 67 | +// root.left.left = new TreeNode(1); |
| 68 | +// root.left.right = new TreeNode(3); |
| 69 | +// root.right = new TreeNode(-3); |
| 70 | +// root.right.left = new TreeNode(-2); |
| 71 | +// root.left.left.left = new TreeNode(-1); |
| 72 | +// int sum = 2; |
| 73 | +// 1 |
| 74 | +// / \ |
| 75 | +// -2 -3 |
| 76 | +// / \ / |
| 77 | +// 1 3 -2 |
| 78 | +// / |
| 79 | +// -1 |
| 80 | + |
| 81 | + TreeNode root = new TreeNode(5); |
| 82 | + root.left = new TreeNode(4); |
| 83 | + root.left.left = new TreeNode(11); |
| 84 | + root.left.left.left = new TreeNode(7); |
| 85 | + root.left.left.right = new TreeNode(2); |
| 86 | + root.right = new TreeNode(8); |
| 87 | + root.right.left = new TreeNode(13); |
| 88 | + root.right.right = new TreeNode(4); |
| 89 | + root.right.right.left = new TreeNode(5); |
| 90 | + root.right.right.right = new TreeNode(1); |
| 91 | + int sum = 22; |
| 92 | +// 5 |
| 93 | +// / \ |
| 94 | +// 4 8 |
| 95 | +// / / \ |
| 96 | +// 11 13 4 |
| 97 | +// / \ / \ |
| 98 | +// 7 2 5 1 |
| 99 | + List<List<Integer>> res = test.pathSum(root, sum); |
| 100 | + CommonUtils.printIntegerList(res); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments