[leetcode]113. Path Sum II@Java解题报告
来源:互联网 发布:hbo 知乎 编辑:程序博客网 时间:2024/06/05 12:42
https://leetcode.com/problems/path-sum-ii/description/
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]]
package go.jacob.day808;import java.util.ArrayList;import java.util.List;/** * 解法类似Path sum。多了保存结果的操作 * @author Jacob * */public class Demo3 {//res保存结果List<List<Integer>> res ;public List<List<Integer>> pathSum(TreeNode root, int sum) {res = new ArrayList<List<Integer>>();if (root == null)return res;List<Integer> list=new ArrayList<Integer>();getPath(list,root,sum);return res;}private void getPath(List<Integer> list,TreeNode root, int sum) {if(root==null)return;list.add(root.val);if(root.val==sum&&root.left==null&&root.right==null)res.add(new ArrayList<Integer>(list));getPath(list,root.left,sum-root.val);getPath(list,root.right,sum-root.val);//移除最后一个元素list.remove(list.size()-1);}}
阅读全文
0 0
- [leetcode] 113. Path Sum II 解题报告
- [Leetcode] 113. Path Sum II 解题报告
- [leetcode]113. Path Sum II@Java解题报告
- 【LeetCode】Path Sum II 解题报告
- [LeetCode] Path Sum II 解题报告
- Leetcode 113. Path Sum II 路径和2 解题报告
- [leetcode]112. Path Sum@Java解题报告
- [LeetCode]Path Sum,解题报告
- 【LeetCode】Path Sum 解题报告
- [leetcode]124. Binary Tree Maximum Path Sum@Java解题报告
- 【leetcode】113. Path Sum II【java】
- [LeetCode]Minimum Path Sum,解题报告
- 【LeetCode】Minimum Path Sum 解题报告
- [leetcode] 64. Minimum Path Sum 解题报告
- [leetcode] 112. Path Sum 解题报告
- [LeetCode] Minimum Path Sum 解题报告
- LeetCode 64. Minimum Path Sum 解题报告
- LeetCode Oj 112. Path Sum 解题报告
- 如何删除GIT中的.DS_Store
- PHP控制反转(IOC)和依赖注入(DI)
- IntelliJ IDEA 编辑窗口开多后自动关闭问题
- Matching In Multiplication(HDU 6073)
- js中莫名奇妙的报The left-hand side of an assignment must be a variable错误
- [leetcode]113. Path Sum II@Java解题报告
- 一个三年程序员的博客开始之路
- DSP的CMD文件解读
- target\m2e-wtp\web-resources\META-INF\MANIFEST.MF (系统找不到指定的路径。)
- DFS A题
- 多重继承,钻石继承和虚继承
- leetcode--Compare Version Numbers
- 优化VoIP语音质量
- Kotlin&Anko基础教程大全 一