//注意,1,要判断null;2,要注意ArrayList直接复制会被一起改变。要通过new的方式来操作。
public class Solution {
public static void main(String[] args){
TreeNode root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(12);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(7);
ArrayList<ArrayList<Integer>> test = FindPath(null,22);
for(ArrayList<Integer> tmp : test){
System.out.println(tmp);
}
}
public static ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
ArrayList<ArrayList<TreeNode>> res = new ArrayList(); ArrayList<ArrayList<Integer>> ans = new ArrayList();
if(root == null || root.val > target ) return ans;
ArrayList<TreeNode> tmp = new ArrayList();
tmp.add(root);
res.add(tmp);
int flag = 1;
while(flag != 0){
System.out.println(123);
flag = 0;
ArrayList<ArrayList<TreeNode>> res2 = new ArrayList();
for(ArrayList<TreeNode> a : res){
ArrayList<TreeNode> left = new ArrayList(a);
ArrayList<TreeNode> right = new ArrayList(a); TreeNode tmp1 = a.get(a.size()-1).left;
TreeNode tmp2 = a.get(a.size()-1).right;
if(tmp1 != null ){
System.out.println("tmp1=" + tmp1.val);
left.add(tmp1);
res2.add(left); flag = 1;
} if(tmp2 != null ){
right.add(tmp2);
res2.add(right);
flag = 1;
}
if(tmp1 == null && tmp2 == null){
res2.add(left);
} }
res = new ArrayList(res2);
for(ArrayList<TreeNode> a : res){
for(TreeNode t : a){
System.out.print(t.val+" ");
}
System.out.println("");
}
System.out.println(flag);
System.out.println("res.size()="+ res.size());
} for(ArrayList<TreeNode> a : res){
if(sum(a) == target){
ArrayList<Integer> al = new ArrayList();
for(TreeNode t : a){
al.add(t.val);
} ans.add(al);
}
} return ans;
} public static int sum (ArrayList<TreeNode> tmp){
int sum = 0;
for(TreeNode t : tmp){
sum += t.val;
}
return sum;
}
}

4.9---二叉树路径和(CC150)的更多相关文章

  1. [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 ...

  2. [LeetCode] 113. 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 ...

  3. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  4. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  5. LeetCode 257. Binary Tree Paths (二叉树路径)

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  6. [leetcode]257. Binary Tree Paths二叉树路径

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  7. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...

  8. [LeetCode] 257. Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. PTA 二叉树路径

    二叉树的路径 (25 分) 二叉树是一种普通的数据结构.给出一棵无限的二叉树,节点被标识为一对整数,构造如下:     (1)树根被标识为整数对(1,1).     (2)如果一个节点被标识为(a,b ...

  10. LeetCode 112. Path Sum (二叉树路径之和)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

随机推荐

  1. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【十】——使用CacheCow和ETag缓存资源

    系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 本文将使用一个开源框架CacheCow来实现针对Http请求资源缓存,本文主要介绍服务器端的 ...

  2. 第1章 JavaScript简介

    概述:Javascript是一种依赖于网页浏览器的脚本语言.是一种脚本语言.由Netscape和Sun共同开发.与Java没有什么关系.作为一种语言标准,而被称为ECMACcript.一个JS的实现包 ...

  3. asp+mysql__不同类型用户登录

    未防注入//0.0 /***这里代码应用场景为多类用户登录,根据用户选择不同的单选按钮判断用户登录的类型,*从而进行不同的数据表进行判断,用户的用户名和密码是否正确.*/ public partial ...

  4. IIS发布WCF遇到的问题总结

    1.安装.NET Framework和VS以后才装的,ASP.NET也没装上,所以一般情况下,只需要运行那个众所周知的“aspnet_regiis -i”命令就行了.但是Windows 8上这个命令是 ...

  5. python 传递结构体指针到 c++ dll

    CMakeLists.txt # project(工程名) project(xxx) # add_library(链接库名称 SHARED 链接库代码) add_library(xxx SHARED ...

  6. js获取iframe里的body内容

    做个页面 需要加入a.html 使用的js动态添加iframe 直接JQ添加的 代码 $(".banner-box").after(“<iframe src="ht ...

  7. Python之路【第五篇续】:面向对象编程二

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABgQAAALaCAIAAABxja8cAAAgAElEQVR4nOzd6X9Tdd74/+uv+f5uzF

  8. CSS学习点滴

    1.CSS :link 选择器 a:link { background-color:yellow;text-decoration:none } 参考:http://www.w3school.com.c ...

  9. Python开发【第二篇】:初识Python

    Python开发[第二篇]:初识Python   Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏 ...

  10. idea的修改文件变颜色

    直接用IntelliJ IDEA 拉SVN 工程 时,如下图所示都不勾选可以成功拉取.