//注意,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. Java多线程系列

    一.参考文献 1.:Java多线程系列目录 (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式 03. ...

  2. $.ajax()引发的对Deferred的总结 (转)

    传统的ajax写法: $.ajax({ url:"1.json", type:"get", success:function(data){}, error:fu ...

  3. echosp 销量排行 新增实际价格

    找到lib_goods.php第147行,代码 $sql = 'SELECT g.goods_id, g.goods_name, g.shop_price,g.goods_thumb, SUM(og. ...

  4. 在Linux主机上搭建SVN,用于同步提交修改,实现本地提交线上预览(SVN Hook功能实现)

    原文地址: http://blog.csdn.net/ROVAST/article/details/44887707?ref=myread 注:上文中钩子使用中有错误,正确的使用如下,上文中忘记了up ...

  5. SignalR 资料

    链接:http://www.cnblogs.com/royding/p/3919134.html

  6. asp.net MVC动态路由

    项目中遇到需要动态生成控制器和视图的. 于是就折腾半天,动态生成控制器文件和视图文件,但是动态生成控制器不编译是没法访问的. 找人研究后,得到要领: 1.放在App_Code文件夹内 2.不要命名空间 ...

  7. Linux命令之dos2unix

    Linux命令之dos2unix (2011-09-22 11:24:06) 转载▼ 标签: 杂谈   Linux命令之dos2unix - 将DOS格式文本文件转换成UNIX格式 用途说明 dos2 ...

  8. linux访问windows共享文件夹的方法

    博客转自:http://www.01happy.com/linux-access-windows-shares-folders/ 有时需要在linux下需要访问windows的共享文件夹,可以使用mo ...

  9. svn 切出指定版本、更改版本名称、删除分支

    1,切出指定版本 svn copy svn://192.168.1.52/help/branches/help_forShop_140307 -r 170 svn://192.168.1.52/hel ...

  10. edwin报警和监控平台开源了(python源码)

    简单介绍一下edwin edwin是一个报警和监控平台, 可以使用它监控任意东西, 如有异常(分为警告级和严重级), 可以发出报警. 可以自定义报警的通知方式, 比如邮件/短信/电话. 另外, 它提供 ...