//注意,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. Linux 命令小记

    1. pidof 进程名 :获取进程的pid,例如 pidof memcached 得到5333 2. unset Shell变量 :取消设置一个shell变量,从内存和shell的导出环境中删除它, ...

  2. linux 开机启动过程详解

    Linux开机执行内核后会启动init进程,该进程根据runlevel(如x)执行/etc/rcx.d/下的程序,其下的程序是符号链接,真正的程序放在/etc/init.d/下.开机启动的程序(服务等 ...

  3. 一段发工资的shell代码

    人事发工资条之前是一个个截图发到我们的邮箱里,看人事妹纸是一个善良而又美丽的姑凉,于是乎写了一段shell代码实现批量发短信至各个手机号.不多说了,上代码,其实很简单,我都不好意思上传,还是记录下吧, ...

  4. sufeinet

    http://www.sufeinet.com http://tool.sufeinet.com/

  5. sublime设置

    修改字体: "font_face": "Monaco", "font_size": 14.0

  6. safari 调用隐藏fileInput

    在safari上,用自定义按钮调用隐藏fileInput,注意点 1. event listener中,不要 return false2. 不要使用display:none,可使用 opacity:0 ...

  7. 开源项目管理平台*redmine*的架设

    yum -y install ruby yum install rubygems gem install heroku gem install rack -v=1.0.1 gem install ru ...

  8. 离屏渲染学习笔记 /iOS圆角性能问题

    离屏渲染学习笔记 一.概念理解 OpenGL中,GPU屏幕渲染有以下两种方式: On-Screen Rendering 意为当前屏幕渲染,指的是GPU的渲染操作是在当前用于显示的屏幕缓冲区中进行. O ...

  9. word中那些重要但是被人忽略的快捷键和长word文档的跳转

    重复上一次操作: F4, 这个太重要了,比如你在做一次很复杂的操作, 下一次又要这样操作时就很有用! 如设置 文字的 段落背景/ 底纹颜色!时要多次设置这个时就 非常有用! 段落缩进:ctrl+M : ...

  10. 该不该用inline-block取代float? inline和float的区别?

    该不该用inline-block取代float? 请看这篇文章引用: jtyjty99999的博客 让块级元素 水平排列的通常方式是float, 但是float可能会带来很多意外的问题 可以考虑用in ...