leetcode — path-sum-ii
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的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- [leetcode]Path Sum II @ Python
原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...
- 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 ...
- 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 ...
- [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 ...
- LeetCode Path Sum II (DFS)
题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...
- 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 ...
随机推荐
- rhel 7安装Mysql
rhel7安装mysql服务 环境: 1)rhel 7虚拟机 2)配置完163网络yum源,并且保证网络通畅 安装过程: 1) 安装Mysql和Mysql-devel 命令:yum install m ...
- (转)CentOS7中防火墙的基本操作
目录 1.firewalld简介 2.安装firewalld 3.运行.停止.禁用firewalld 4.配置firewalld 5 打开端口 学习apache安装的时候需要打开80端口,由于cent ...
- [Ubuntu]Firefox书签Ubuntu与Windows同步
Ubuntu默认使用Firefox国际版.其他平台访问官网下载到的都是中国版,而国际版和中国版使用两套账号体系,相互之间无法同步,导致Ubuntu的Firefox无法和其他平台的Firefox同步书签 ...
- LOJ 6092
这个题也很没意思 发现q那么大没有用, 不重复的询问有26*n种 所以记录一下就好了 #include<bits/stdc++.h> using namespace std; #defin ...
- 七牛云音频转码准备工作之如何创建音视频处理私有队列pipeline
如何创建音视频处理私有队列 最近更新时间:2017-08-28 15:54:45 在七牛进行音视频处理,推荐使用私有队列(pipeline). 创建私有队列方法如下: 第一步 登录七牛开发者平台 ht ...
- c# 集合的长度为什么是可变的
摘要: 写在前面:此随笔仅仅是作为个人学习总结,有不对的地方,请各位前辈指正O(∩_∩)O........ 一: 引入 在学习集合之前我们都学习过数组.可以知道数组的长度在声明的时候就已经被固定了,不 ...
- (一) sublime安装和使用
1 下载安装sublime 可以破解也可以不破解 2 html基础架子自动生成插件Emmet的安装 3 Emmet 安装失败解决 4 快捷键设置和汇总 4 其他sublime插件汇总
- maven <include>与<exclude>划定的范围存在冲突,则以<exclude>配置为准。
maven 与划定的范围存在冲突,则以配置为准.
- python elasticsearch 批量写入数据
from elasticsearch import Elasticsearch from elasticsearch import helpers import pymysql import time ...
- js深度复制三种方法
1.用递归的方式进行深度复制 2.用JSON.stringify加上JSON.parse()进行深度复制 3.用jquery中自带的方法$.extend()进行深度复制 具体实现代码可百度自行查询