题目

给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。

一个有效的路径,指的是从根节点到叶节点的路径。

解题

下面有个小bug

最后比较的时候是叶子节点为空,左右都有叶子结点,所有会出现重复的情况,聪明的你可能会想到保留不重复的结果

但是但一个树的结点都相同时候就不可以了

两层,三个结点,每个节点都是1,路径和是2

这样就有两个个答案[1,1]、[1,1]

所以再是叶子结点时候就要进行判断,这里的叶子结点要是真叶子节点,左右节点为空而自己不空

public class Solution {
/**
* @param root the root of binary tree
* @param target an integer
* @return all valid paths
*/
public List<ArrayList<Integer>> binaryTreePathSum(TreeNode root, int target) {
// Write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
List<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
PathSum(root,target,result,list);
return result;
}
public void PathSum(TreeNode root,int target,List<ArrayList<Integer>> result,ArrayList<Integer> list){
if(target ==0 && root==null){
ArrayList<Integer> l = new ArrayList<Integer>(list);
if(!result.contains(l))
result.add(l);
return;
}
if( root==null)
return;
int val = root.val;
list.add(val); ArrayList<Integer> list2 = new ArrayList<Integer>(list);
    
PathSum(root.left,target-val,result,list);
     list.remove(list.size()-1);
PathSum(root.right,target-val,result,list2);
list2.remove(list2.size()-1);
}
}

AC代码

public class Solution {
/**
* @param root the root of binary tree
* @param target an integer
* @return all valid paths
*/
public List<ArrayList<Integer>> binaryTreePathSum(TreeNode root, int target) {
// Write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
List<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
PathSum(root,target,result,list);
return result;
}
public void PathSum(TreeNode root,int target,List<ArrayList<Integer>> result,ArrayList<Integer> list){
if( root==null)
return; if( (target ==root.val) && root.left==null && root.right==null){
list.add(root.val);
ArrayList<Integer> l = new ArrayList<Integer>(list);
// if(!result.contains(l))
result.add(l);
return;
} int val = root.val;
list.add(val); ArrayList<Integer> list2 = new ArrayList<Integer>(list);
PathSum(root.left,target-val,result,list);
list.remove(list.size()-1);
PathSum(root.right,target-val,result,list2);
list2.remove(list2.size()-1);
}
}

lintcode:二叉树的路径和的更多相关文章

  1. Java实现求二叉树的路径和

    题: 解: 这道题考的是如何找出一个二叉树里所有的序列. 我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和. 这样 ...

  2. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  3. [LeetCode] Path Sum IV 二叉树的路径和之四

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  4. LintCode-376.二叉树的路径和

    二叉树的路径和 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径. 一个有效的路径,指的是从根节点到叶节点的路径. 样例 给定一个二叉树,和 目标值 = 5: 返回: [      ...

  5. [LeetCode] 666. Path Sum IV 二叉树的路径和 IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  6. lintcode:二叉树的所有路径

    二叉树的所有路径 给一棵二叉树,找出从根节点到叶子节点的所有路径. 样例 给出下面这棵二叉树: 1 / \ 2 3 \ 5 所有根到叶子的路径为: [ "1->2->5" ...

  7. [LeetCode] Path Sum 二叉树的路径和

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

  8. lintcode二叉树的锯齿形层次遍历 (双端队列)

    题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...

  9. LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)

    题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...

随机推荐

  1. AutoCAD/Civil 3D 学习笔记

    Civil学习笔记 1.环境配置 1.添加引用: Civil二次开发需要5个基本的AutoCAD的dll引用-acdbmgd.dll, acmgd.dll, accoremgd.dll, AecBas ...

  2. perl连接mysql(转载)

    文章来源:http://blog.sina.com.cn/s/blog_9d0445d50101czsr.html 首先需要用ppm安装DBI和DBD-mysql ,如果没有的话点击EDIT-pref ...

  3. R统计图

    主题:R统计图 作者:luomg 关键字:统计,R,ggplot2 1.什么是统计图? 统计图:统计图是从数据到几何对象的图形属性的一个映射 砖石重量对价格的散点图 qplot(carat,price ...

  4. Qt使用QStackedWidget实现堆栈窗口

    Qt使用QStackedWidget实现堆栈窗口 分类: QT2012-07-25 21:59 6997人阅读 评论(0) 收藏 举报 qtlistsignal 堆栈窗口可以根据选择项的不同显示不同的 ...

  5. Actionform

    Actionform 2013年7月8日 15:08 Reset 用actionform是把数据恢复到初始状态 Getter/setter Validate 验证 已使用 Microsoft OneN ...

  6. Binary Tree Zigzag Level Order Traversal

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  7. algorithm之改变序列算法--待解决

    简述:改变序列算法,参见http://www.cplusplus.com/reference/algorithm/?kw=algorithm 待解决问题:iterator_traits.std::mo ...

  8. VBS基础篇 - 内置函数

    Date/Time 函数 函数 描述 CDate 把有效的日期和时间表达式转换为日期(Date)类型. Date 返回当前的系统日期. DateAdd 返回已添加指定时间间隔的日期. DateDiff ...

  9. 小组开发项目NABC分析

    我们团队的开发项目为:重量解锁 是根据重力感应实现手机的解锁方式,在传统滑屏的基础上我们想增添新的形式,实现用户用一组动作就能实现手机解锁功能,更加方便,炫酷. NABC模型 1.N:我们的创意在使用 ...

  10. 【每日scrum】NO.6

    Yesterday:组内各种乱八七糟的问题,还有自己的效率问题 Today:进行小范围的测试实验 Problem:在显示各景点构成的邻接矩阵的时候,第一次编译未出现任何错误的提示,但是在程序运行时,无 ...