|
| 1 | +packagemedium; |
| 2 | + |
| 3 | +importjava.util.ArrayList; |
| 4 | +importjava.util.List; |
| 5 | + |
| 6 | +importutils.CommonUtils; |
| 7 | +importclasses.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 | +publicclassPathSumII { |
| 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 | +publicList<List<Integer>>pathSum(TreeNoderoot,intsum) { |
| 33 | +List<List<Integer>>allPaths =newArrayList(); |
| 34 | +if(root ==null)returnallPaths; |
| 35 | +List<Integer>path =newArrayList(); |
| 36 | +dfs(root,path,allPaths,sum); |
| 37 | +returnallPaths; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +privatevoiddfs(TreeNoderoot,List<Integer>path,List<List<Integer>>allPaths,intsum) { |
| 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 =newArrayList(path); |
| 52 | +allPaths.add(onePath); |
| 53 | + } |
| 54 | + } |
| 55 | +path.remove(path.size()-1); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +publicstaticvoidmain(String...strings){ |
| 60 | +PathSumIItest =newPathSumII(); |
| 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 | +TreeNoderoot =newTreeNode(5); |
| 82 | +root.left =newTreeNode(4); |
| 83 | +root.left.left =newTreeNode(11); |
| 84 | +root.left.left.left =newTreeNode(7); |
| 85 | +root.left.left.right =newTreeNode(2); |
| 86 | +root.right =newTreeNode(8); |
| 87 | +root.right.left =newTreeNode(13); |
| 88 | +root.right.right =newTreeNode(4); |
| 89 | +root.right.right.left =newTreeNode(5); |
| 90 | +root.right.right.right =newTreeNode(1); |
| 91 | +intsum =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 | +} |