Description

Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target.

A valid path is from root node to any of the leaf nodes.

Example

Given a binary tree, and target = 5:

     1
/ \
2 4
/ \
2 3

return

[
[1, 2, 2],
[1, 4]
]

解题:给一个二叉树,找到和是给定目标值的支路。如果不用栈,需要考虑一下回溯算法的运用,代码如下:

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/*
* @param root: the root of binary tree
* @param target: An integer
* @return: all valid paths
*/
int target = 0;
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
// write your code here
if(root == null){
return res;
}
this.target = target;
List<Integer>path = new ArrayList<>();
helper(path, root, 0);
return res;
}
public void helper(List<Integer>path, TreeNode root, int sum){
sum +=root.val;
path.add(root.val);
//如果到叶子结点了,并且和为target
if(root.left == null && root.right == null && sum == target){
ArrayList<Integer>temp = new ArrayList<>();
temp.addAll(path);//复制一份
res.add(temp);
}
if(root.left != null){
helper(path, root.left, sum);
}
if(root.right != null){
helper(path, root.right, sum);
}
path.remove(path.size() - 1);
}
}

376. Binary Tree Path Sum【LintCode java】的更多相关文章

  1. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  2. 375. Clone Binary Tree【LintCode java】

    Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...

  3. Binary Tree Path Sum

    Given a binary tree, find all paths that sum of the nodes in the path equals to a given number targe ...

  4. leetcode:Path Sum【Python版】

    1.类中递归调用函数需要加self # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # s ...

  5. 245. Subtree【LintCode java】

    Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...

  6. 408. Add Binary【LintCode java】

    Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...

  7. 365. Count 1 in Binary【LintCode java】

    Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return  ...

  8. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  9. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

随机推荐

  1. 用DecimalFormat格式化十进制数字的实际应用

    在项目中,有时候我们需要将数字转换成特定的格式便于操作和使用.最常用的就是在操作价格数字的时候,需要将数字转换成小数点后保留两位小数,比如讲3.4转换成3.40 我们可以用DecimalFormat, ...

  2. iOS 8及以后版本 如何创建UIAlertView?

    1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...

  3. MyEclipse 根据左括号或右括号查找另外一半

    在MyEclipse 中如果代码嵌套太多,查找括号是一件非常头疼的事情,今天突然发现了一个快捷键(如下)可以直接定位到另外一半的位置 Ctrl+Shift+P,光标会自动跳到相应的花括号位置, 并且可 ...

  4. SQLlearn

  5. Linux系统NBD驱动安装拓展篇

    前言: 最近在安装中标麒麟机器的时候,发现麒麟的操作系统找不到src.rpm包,且系统内部也没有内核文件,导致正常方法安装NBD驱动无法实施.故这里找了另一种办法帮助此类型操作系统安装NBD驱动. 一 ...

  6. Django学习笔记4-csrf防护

    1.CSRF验证失败. 请求被中断. 原因是django为了在用户提交表单时防止跨站攻击所做的保护 什么是 CSRF CSRF, Cross Site Request Forgery, 跨站点伪造请求 ...

  7. Flask中那些特殊的装饰器

    模板相关的装饰器 @app.template_global() 用法: @app.template_global() # 记得加括号 def jiafa(a, b): # 这个方法每调用一次就需要传一 ...

  8. h5禁用手机input点击放大

    最近项目做的是h5的手机移动端,在用苹果浏览器测试时,弹出框输入信息会自动拉伸屏幕,并且不会像安卓一样回来. 网上查找说设置浏览器自适应头,但是并没有效果, <meta name="v ...

  9. 大数据学习--day03(运算符、流程控制语句)

    运算符.流程控制语句 自增自减容易出错的地方: 扩展的赋值运算符  a+=b 等同于 a = a+b;  扩展的赋值运算符 隐含了一个类型的强制转换 & && 有何区别   & ...

  10. OpenWrt超时检测

    参考http://www.right.com.cn/forum/thread-261702-1-1.html vim /home/ihid/chaos_calmer/feeds/luci/module ...