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. vue-cli 2篇官方文档记录收藏

    https://cli.vuejs.org/guide/ http://vuejs-templates.github.io/webpack/

  2. ubuntu系统下matplotlib中文乱码问题

    参考 [ubuntu系统下matplotlib中文乱码问题 - CSDN博客](https://blog.csdn.net/jeff_liu_sky_/article/details/54023745 ...

  3. 完整版本的推箱子小游戏,最简单的纯C语言打造

    /* 推箱子小游戏 1.定义绘制样式 用二维数组的方式 2.绘制图像 3.找出当前位置 4.逻辑判断,制造动作 根据数学xy轴的规律,这里使用ij 上移,行轴上升,行数减少 下移,行数下降,函数增加 ...

  4. centos7搭建zabbix3.0监控系统

    关闭防火墙和selinux systemctl stop firewalld.service                (停止防火墙) systemctl disable firewalld.se ...

  5. Git Log描述乱码问题解决方法

    在git bash 中执行以下命令:git config --global core.quotepath off git config --global --unset i18n.logoutpute ...

  6. Mysql初学入门

    最近研究了一下Mysql的初学应用,在此进行整理记录. 1.Windows系统下的安装 我用的是win10系统,在http://dev.mysql.com/downloads/mysql/ 下载相应版 ...

  7. js 单行注释

    不可以: var a = 1;//这是注释 应当: var a = 1; //这是注释 1

  8. Java语法细节 - synchronized和volatile

    目录 synchronized关键字 关键字volatile synchronized关键字 synchronized关键字锁住方法和this的不同之处: public synchronized vo ...

  9. Web版记账本开发记录(三)开发过程遇到的问题小结2

    问题1,获得当前时间 Date d = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ...

  10. 11、js 数组详细操作方法及解析合集

    js 数组详细操作方法及解析合集 前言 在开发中,数组的使用场景非常多,平日中也涉及到很多数组的api/相关操作,一直也没有对这块内容进行一块整理总结,很多时候就算用过几次这个api,在开发中也很容易 ...