4.9---二叉树路径和(CC150)
//注意,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)的更多相关文章
- [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] 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 ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [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 ...
- UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)
Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- PTA 二叉树路径
二叉树的路径 (25 分) 二叉树是一种普通的数据结构.给出一棵无限的二叉树,节点被标识为一对整数,构造如下: (1)树根被标识为整数对(1,1). (2)如果一个节点被标识为(a,b ...
- 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 ...
随机推荐
- DB服务器中的参数优化
1.swappiness 禁止系统使用swap空间,配置/etc/sysctl.conf中的vm.swappiness=0 2.Scheduler调度 Scheduler调度,指的是磁盘的IO调度算法 ...
- VIM的姿势
http://blog.csdn.net/vincent_czz/article/details/7900670 http://bbs.feng.com/read-htm-tid-7435912.ht ...
- 取出return array() 数组内容
d.php文件 return array( "0" => 内容一, "1" => 内容二, "2" => 内容三, &qu ...
- python实用笔记,加快编程速度,lamdba,三元运算,open.
lamdba 表达式. # f1=lamdba x:x+1 三元运算 # b=True if 1 < 2 else False with op ...
- Java8初体验(一)lambda表达式语法
感谢同事[天锦]的投稿.投稿请联系 tengfei@ifeve.com 本文主要记录自己学习Java8的历程,方便大家一起探讨和自己的备忘.因为本人也是刚刚开始学习Java8,所以文中肯定有错误和理解 ...
- 『MySQL』索引类型 normal, unique, full text
问题1:mysql索引类型normal,unique,full text的区别是什么? normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用 ...
- Typecho中的gravatar头像无法加载
将var/Typecho/Common.php中的第939行中的http://www.gravatar.com/中的www.给去掉即可! //修改前 $url = $isSecure ? 'https ...
- jQuery1.9.1源码分析--数据缓存Data模块
jQuery1.9.1源码分析--数据缓存Data模块 阅读目录 jQuery API中Data的基本使用方法介绍 jQuery.acceptData(elem)源码分析 jQuery.data(el ...
- JUnit之持续集成(CI,Continuous Integration)
序,测试驱动开发告诉我们,要尽早测试,经常测试.如果我们进行一点小改动时,都把所有的单元测试.集成测试和功能测试执行一遍,这就会非常浪费时间.为了避免这一点,在开发期间我们只执行单元测试,那么集成测试 ...
- Jquery 操作IFrame
使用jquery操作iframe 1. 内容里有两个ifame <iframe id="leftiframe"...</iframe> <iframe id ...