import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/path-sum-ii/
*
*
* 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]
* ]
*
*/
public class PathSum2 { /**
*
* 求出根节点到叶子节点的和等于sum的所有路径
*
* @param root
* @param target
* @return
*/
public List<List<TreeNode>> pathSum (TreeNode root, int target) {
List<List<TreeNode>> result = new ArrayList<List<TreeNode>>();
List<TreeNode> path = new ArrayList<TreeNode>();
pathSum(root, result, path, 0, target);
return result;
} public void pathSum (TreeNode root, List<List<TreeNode>> result, List<TreeNode> path, int sum, int target) {
if (root == null) {
if (sum == target) {
List<TreeNode> list = new ArrayList<TreeNode>(path);
result.add(list);
}
return ;
}
path.add(root);
if (root.leftChild != null || root.rightChild != null) {
// 如果左右子节点都为空只计算一个
pathSum(root.leftChild, result, path, sum + root.value, target);
} pathSum(root.rightChild, result, path, sum + root.value, target);
path.remove(path.size()-1); } public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
} private static void print (List<List<TreeNode>> list) {
for (List<TreeNode> nodes: list) {
System.out.println(Arrays.toString(nodes.toArray(new TreeNode[nodes.size()])));
}
System.out.println();
} private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value; public TreeNode(int value) {
this.value = value;
} public TreeNode() { } @Override
public String toString() {
return value + "";
}
} public static void main(String[] args) {
PathSum2 pathSum2 = new PathSum2();
char[] arr0 = new char[]{'5','4','8','1','#','3','3','7','2','#','#','5','1'}; print(pathSum2.pathSum(pathSum2.createTree(arr0), 12));
print(pathSum2.pathSum(pathSum2.createTree(arr0), 17));
}
}

leetcode — path-sum-ii的更多相关文章

  1. [leetcode]Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  2. LeetCode: Path Sum II 解题报告

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  3. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  4. [Leetcode] Path Sum II路径和

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  5. [leetcode]Path Sum II @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...

  6. leetcode: Path Sum II 迭代法

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  7. LeetCode——Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  8. [LeetCode] Path Sum II 深度搜索

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  10. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

随机推荐

  1. linux服务器用ssh 公钥下载代码 创建公钥,添加

    ssh-keygen -t rsa -C "8....@qq.com"cd ~/.sshcat id_rsa.pub 粘贴到对应的 https://github.com/setti ...

  2. 2018 Multi-University Training Contest 3 - HDU Contest

    题解: solution Code: A. Ascending Rating #include<cstdio> const int N=10000010; int T,n,m,k,P,Q, ...

  3. 用JDBC把Excel中的数据导入到Mysql数据库中

    步骤:0.在Mysql数据库中先建好table 1.从Excel表格读数据 2.用JDBC连接Mysql数据库 3.把读出的数据导入到Mysql数据库的相应表中 其中,步骤0的table我是先在Mys ...

  4. 安装selenium

    步骤: 1.下载setuptools2.点击安装setuptools.exe3.安装成功后,python的安装目录下会有Scripts的文件夹4.配置环境变量:python安装目录\Scripts5. ...

  5. Hive+Sqoop+Mysql整合

    Hive+Sqoop+Mysql整合 在本文中,LZ随意想到了一个场景: 车,道路,监控,摄像头 即当一辆车在道路上面行驶的时候,道路上面的监控点里面的摄像头就会对车进行数据采集. 我们对采集的数据进 ...

  6. AES加密算法详解

    AES 是一个对称密码分组算法,分组长度为128bit,密钥长度为128.192 和 256 bit. 整个加密过程如下图所示. 1.密钥生成算法 密钥扩展过程: 1)  将种子密钥按下图所示的格式排 ...

  7. JS获取键盘事件

    <script type="text/javascript" language=JavaScript charset="UTF-8"> docume ...

  8. NOIP2017感悟

    Day1 第一次会做的题这么多却比以前靠的更差. 其实停课期间水平还是提升了很多,但是做题速度和心态问题一就是我最难克服的一个地方. 题目很简单,但是又很恶心,主要是我代码能力太差,第二题调不出来,第 ...

  9. html笔记第一天

    快速生成标签有序ol>li*3无序ul>(li>a{新闻标题})*3定义列表 dl>(dt+dd)*3制作表格table>(tr>td*5)*6pading:3个数 ...

  10. python+SQLAlchemy+爬虫

    python+SQLAlchemy+爬虫 前面分享了SQLAlchemy的知识,这次我共享一下学习用python开发爬虫再把爬出来的数据放到用SQLAlchemy的数据库上面的知识,当然我这个是带测试 ...