leetcode 113. Path Sum II
2016-03-28 20:10
316 查看
Question
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.For example:
Given the below binary tree and sum = 22,
5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
Code
public List<List<Integer>> pathSum(TreeNode root, int sum) { List<List<Integer>> rst = new ArrayList<>(); List<Integer> solution = new ArrayList<Integer>(); findSum(rst, solution, root, sum); return rst; } private void findSum(List<List<Integer>> result, List<Integer> solution, TreeNode root, int sum) { if (root == null) { return; } sum -= root.val; if (root.left == null && root.right == null) { if (sum == 0) { solution.add(root.val); result.add(new ArrayList<Integer>(solution)); solution.remove(solution.size() - 1); } return; } solution.add(root.val); findSum(result, solution, root.left, sum); findSum(result, solution, root.right, sum); solution.remove(solution.size() - 1); }
相关文章推荐
- HK共享吧解压密码
- 云通信-腾讯云,TLS独立模式公私钥生成
- HK共享吧解压密码
- selenium在远程服务器上的虚拟显示
- Android SwipeRefreshLayout 下拉刷新组件的使用
- 设计模式--单例模式
- React-Native 之Android应用开发踩坑纪 (一)————windows环境下配置
- 缺失值处理方法
- 滴滴出行iOS客户端架构演进之路
- 时间类2
- 实现ViewPager懒加载的三种方法
- Leetcode 112. Path Sum
- POJ 2752 Seek the Name, Seek the Fame(next数组的应用)
- 第五周项目3时间类(1)
- 架构师必看 京东咚咚架构演进
- IO流
- 关于Swift中出现dyld: Library not loaded: @rpath/libswiftCore.dylib错误的解决方法
- LAMP环境配置初体验
- 第5周-项目3-时间类(2)
- laserscan_multi_merger代码解析