leetcode 113 Path Sum II
来源:互联网 发布:互联网租车平台知豆 编辑:程序博客网 时间:2024/05/22 05:28
采用深度优先与回溯相结合的算法
/**
* 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 LinkedList<List<Integer>>();
List<Integer>curlist=new ArrayList<Integer>();
pathSum(root,sum,res,curlist);
return res;
}
public void pathSum(TreeNode root,int sum,List<List<Integer>>list,List<Integer>curlist){
if(root==null)return;
curlist.add(new Integer(root.val));
if(root.left==null&&root.right==null&&root.val==sum)//叶子节点若满足该条件则为所求的路
{
list.add(new ArrayList(curlist));
curlist.remove(curlist.size()-1);//移除该节点,因为同属于同一个父节点的叶子节点可能与该节点值相同
return;
}
else{
pathSum(root.left,sum-root.val,list,curlist);
pathSum(root.right,sum-root.val,list,curlist);
}
curlist.remove(curlist.size()-1);
}
}
0 0
- LeetCode 113: Path Sum II
- LeetCode(113)Path Sum II
- LeetCode: Path Sum II [113]
- [leetcode 113] Path Sum II
- [LeetCode 113]Path Sum II
- leetcode || 113、Path Sum II
- Leetcode[113]-Path Sum II
- Path Sum II - LeetCode 113
- 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 113:Path Sum II
- LeetCode 113 Path Sum II
- leetcode 113 Path Sum II
- Leetcode 113 Path Sum II
- leetcode 113 Path Sum II
- <学习笔记>数据挖掘原理与运用-sql server2008 数据库
- 操作系统进程ID 分配
- 2016/10/18
- Java+spring切面编程(aop)spring控制反转(ioc)+hibenrate对象关系映射(ORM
- Git使用(三)
- leetcode 113 Path Sum II
- 运算符重载使用
- 【16.56%】【codeforces 687B】Remainders Game
- .Bat之批处理实现手动输入变量参数
- JDBC连接数据库并且实现增删改和查询
- 欢迎使用Markdown编辑器写博客
- Linux上Core Dump文件的形成和分析
- Java的异常处理机制
- 源码阅读之Android创建桌面快捷方式二