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. OneNote中添加代码问题

    OneNote是我最常用的笔记本,然而粘贴代码很麻烦,之前只能屏幕截图如Snipaste自带截图什么的,后来才知道win10自带有win+shift+s自动剪切到草图板上的功能, 然而还是很麻烦. 在 ...

  2. Codechef April Challenge 2019 游记

    Codechef April Challenge 2019 游记 Subtree Removal 题目大意: 一棵\(n(n\le10^5)\)个结点的有根树,每个结点有一个权值\(w_i(|w_i\ ...

  3. vty密码登录,到AAA验证登录,以及远程配置网络

    华为的的最简易的远程登录方式,就是密码登录了. 配置命令如下图: 最重要的是权限: 访问级(0级).监控级(1级).系统级(2级)和管理级(3级) 在以上基础上,做了一个远程配置方式,通过一台,修改其 ...

  4. ntp---时钟同步服务

    NTP--时钟同步服务 地球分为东西十二个区域,共计 24 个时区 格林威治作为全球标准时间即 (GMT 时间 ),东时区以格林威治时区进行加,而西时区则为减. 地球的轨道并非正圆,在加上自转速度逐年 ...

  5. 配置JDK环境变量与配置JRE

    1. 如何配置jdk,x下载jdk     网站: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-21 ...

  6. 到位App_jQuery_art-template

    到位 App 不写 node 服务器,本地模拟 ajax 获取 json 数据 源代码 ---- 参见 ---- 使用 webstorm 运行 index.html 本地静态的 data.json 前 ...

  7. [LeetCode] Fibonacci Number 斐波那契数字

    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...

  8. jsp 表单回显

    1.在表单各个字段中添加value属性,如:value="${user.reloginpass }" 2.在表单提交对应的servlet中封装数据到uer中,如:req.setAt ...

  9. javabean简介

    Javabean简介 JavaBean是一个可重复使用的软件组件.实际上JavaBean是一种Java类,通过封装属性和方法成为具有某种功能或者处理某个业务的对象,简称bean.由于javabean是 ...

  10. js原型与继承

    demofunction Fun(){}
var foo = new Fun();foo.__proto__ === Fun.prototype 摘要 1.js本身不提供类实现,es6引入了class ...