| 4 | public class Question { |
| 5 | |
| 6 | public static void findSum(TreeNode node, int sum, int[] path, int level) { |
| 7 | if (node == null) { |
| 8 | return; |
| 9 | } |
| 10 | |
| 11 | /* Insert current node into path */ |
| 12 | path[level] = node.data; |
| 13 | |
| 14 | int t = 0; |
| 15 | for (int i = level; i >= 0; i--){ |
| 16 | t += path[i]; |
| 17 | if (t == sum) { |
| 18 | print(path, i, level); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | findSum(node.left, sum, path, level + 1); |
| 23 | findSum(node.right, sum, path, level + 1); |
| 24 | |
| 25 | /* Remove current node from path. Not strictly necessary, since we would |
| 26 | * ignore this value, but it's good practice. |
| 27 | */ |
| 28 | path[level] = Integer.MIN_VALUE; |
| 29 | } |
| 30 | |
| 31 | public static int depth(TreeNode node) { |
| 32 | if (node == null) { |