Created
August 28, 2017 01:10
-
-
Save cixuuz/cbc8b7a81a5d6150c51e760786734879 to your computer and use it in GitHub Desktop.
[113. Path Sum II] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<Integer>> pathSum(TreeNode root, int sum) { | |
List<List<Integer>> res = new ArrayList<List<Integer>>(); | |
List<Integer> tmp = new LinkedList<>(); | |
dfs(root, sum, tmp, res); | |
return res; | |
} | |
private void dfs(TreeNode node, int sum, List<Integer> tmp, List<List<Integer>> res) { | |
if ( node == null ) return; | |
tmp.add(node.val); | |
if ( node.left == null && node.right == null && sum == node.val) { | |
res.add(new LinkedList(tmp)); | |
tmp.remove(tmp.size() - 1); | |
return; | |
} | |
dfs(node.left, sum - node.val, tmp, res); | |
dfs(node.right, sum - node.val, tmp, res); | |
tmp.remove(tmp.size()-1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment