[leetCode刷题笔记]113. Path Sum II
2017-06-25 23:46
375 查看
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> pathSum(TreeNode root, int sum) { List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> path = new ArrayList<Integer>(); helper(root, sum, path, res); return res; } private void helper (TreeNode root, int sum, List<Integer> path, List<List<Integer>> res) { if (root == null) { return; } path.add(root.val); if (root.left == null && root.right == null) { if (sum == root.val) { res.add(new ArrayList<Integer>(path)); } path.remove(path.size() - 1); return; } helper(root.left, sum - root.val, path, res); helper(root.right, sum - root.val, path, res); path.remove(path.size() - 1); return; } }
相关文章推荐
- [leetcode] 113. Path Sum II 解题报告
- LeetCode 113. Path Sum II DFS求解
- [LeetCode]112. Path Sum&113. Path Sum II
- leetcode 113. Path Sum II-路径和|回溯算法
- [LeetCode]113. Path Sum II
- 【一天一道LeetCode】#113. Path Sum II
- leetcode 113. Path Sum II 二叉树求符合和值为sum的链
- [leetcode]113. Path Sum II -- JavaScript 代码
- Leetcode 113. Path Sum II 路径和2 解题报告
- 【LeetCode】113. Path Sum II
- 【一天一道LeetCode】#113. Path Sum II
- Leetcode 113. Path Sum II
- [leetcode] 113. Path Sum II
- leetcode 113. Path Sum II (路径和) 解题思路和方法
- [leetcode] @python 113. Path Sum II
- LeetCode *** 113. Path Sum II
- Leetcode 笔记 113 - Path Sum II
- Leetcode-113. Path Sum II
- [leetcode]113. Path Sum II (medium)
- leetCode 113. Path Sum II 二叉树问题 | Medium