lintcode:二叉树的路径和
题目
给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。
一个有效的路径,指的是从根节点到叶节点的路径。
解题
下面有个小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:二叉树的路径和的更多相关文章
- Java实现求二叉树的路径和
		
题: 解: 这道题考的是如何找出一个二叉树里所有的序列. 我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和. 这样 ...
 - [LeetCode] Path Sum III 二叉树的路径和之三
		
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
 - [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 ...
 - LintCode-376.二叉树的路径和
		
二叉树的路径和 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径. 一个有效的路径,指的是从根节点到叶节点的路径. 样例 给定一个二叉树,和 目标值 = 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 ...
 - lintcode:二叉树的所有路径
		
二叉树的所有路径 给一棵二叉树,找出从根节点到叶子节点的所有路径. 样例 给出下面这棵二叉树: 1 / \ 2 3 \ 5 所有根到叶子的路径为: [ "1->2->5" ...
 - [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 ...
 - lintcode二叉树的锯齿形层次遍历 (双端队列)
		
题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...
 - LeetCode  Binary Tree Maximum Path Sum  二叉树最大路径和(DFS)
		
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
 
随机推荐
- Qt5 程序发布打包
			
关于qt5在win7下发布 & 打包 ----------------------------------------------------------------------------- ...
 - win8中如何禁用屏幕旋转的快捷键
			
程序员通常会使用ctrl+alt+方向键 里编辑代码,特别对于使用eclipse的程序员,更是如此,但是win8却把这一快捷键给占用了,很不爽,如何办,很简单.直接上图: 2.但是发现禁用之后并没有解 ...
 - cocos中常用到的单例模式
			
单例:即只有一个类对象,且提供全局的访问权限 特点: 1.构造函数私有 2.私有的静态成员指针,标识是否已产生了单例实例 3.提供一个getInstance()方法来获取单例对象 下面已打飞机中的子弹 ...
 - PHP URL 重定向 的三种方法(转载)
			
为了方便查询,转载一篇. 1.使用header()函数 PHP的HTTP相关函数种提供了一个 header()函数,首先要清楚,header()函数必须放在php程序的开头部分,而且之前不能有另 ...
 - [转载]EF Code First 学习笔记:约定配置
			
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...
 - Ant学习---第二节:Ant添加文件夹和文件夹集的使用
			
一.创建 java 项目(Eclipse 中),结构图如下: 1.创建 .java 文件,代码如下: package com.learn.ant; public class HelloWorld { ...
 - LNMP系列网站零基础开发记录(三)
			
[目录] 扯淡吹逼之开发前奏 Django 开发环境搭建及配置 web 页面开发 Django app开发 Django 站点管理 Python 简易爬虫开发 Nginx&uWSGI 服务器配 ...
 - Careercup - Microsoft面试题 - 6366101810184192
			
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
 - VMM学习-vmm_log
			
功能类似verilog里的$display函数,在vmm里做了强化,可以在仿真过程中看到整个平台的运行信息,用来调试仿真平台. 函数原型在vmm.sv里(class vmm_log;),其构造函数为e ...
 - 【对象模型】C++模版的编译链接过程——编译器真的会检查所有tocken层面的错误么?
			
模版(template)设计的初衷,是设计一种自动实例化机制,不需要使用者参与,编译器可根据使用者提供的模版参数再套用类的定义来实例化.所谓实例化,除了包含对于程序变量的实例化,即开辟空间并设置某些变 ...