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. 【poj Roads in the North】 题解

    题目链接:http://poj.org/problem?id=2631 求树的直径模板. 定理: 树上任意一个点的在树上的最长路一定以树的直径的两端点其中一点结束. 做法: 两边bfs,第一次先找到n ...

  2. 如何在IAR工程中创建和使用模板

    路径为:Edit -> Code Templates -> Edit Templates  如下图: #TEMPLATE "&FileDeclare>&Fi ...

  3. Maven常用的构建命令

    1.mvn -v 查看maven版本 2.mvn compile 编译项目,生成target文件夹,其中包含编译生成的字节码文件和测试报告.打开cmd,cd到项目的根目录,运行该命令如图所示(如果是第 ...

  4. meta的随堂笔记

    Meta标签与搜索引擎优化(SEO) 概要 通常所说的meta标签,是在Html网页源代码中的一个重要的html标签.meta标签用来描述一个html网页文档的属性,例如作者.日期和时间.网页描述.关 ...

  5. 极光推送能获取 registrationId,但是接收不到通知 - iOS

    集成极光推送进行调试的时候,运行 App 可以正常获取 registrationId,但是却迟迟无法收到推送消息,而Android 端是可以正常收到消息; 检查了证书配置和极光的配置一切正常,便开始返 ...

  6. UIPanGestureRecognizer 拖动TableView改变其高度

    需求:项目中要求tableView的高度随着手拖动的位置而改变如下图: 关键代码如下: - (void)viewDidLoad{ panGestureRecognizer = [[UIPanGestu ...

  7. git 的一些基本命令小结

    Git是目前世界上最先进的分布式版本控制系统 对于git 的用法,本文并不属于教程,只是总结记录一些平时用的简单命令 git的下载地址:https://git-scm.com/downloads 主要 ...

  8. Windows 安装 MongoDB 并开启认证

    下载 可以自行上官网找需要的版本,Windows系统各个64位版本下载地址: http://dl.mongodb.org/dl/win32/x86_64 安装 正常的软件安装流程,这里就不细讲了. 配 ...

  9. font(字体)所使用的属性

    1.font-weight:normal blod bolder lighter  100-900之间 400=normal p:first-child{ padding-top: 50px; pos ...

  10. 带提示范围的猜数小游戏--python

    import random random_number = random.randint(1, 99) print(random_number) start_data = 1 end_data = 9 ...