376. Binary Tree Path Sum

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

Example 1:

Input:
{1,2,4,2,3}
5
Output: [[1, 2, 2],[1, 4]]
Explanation:
The tree is look like this:
1
/ \
2 4
/ \
2 3
For sum = 5 , it is obviously 1 + 2 + 2 = 1 + 4 = 5

Example 2:

Input:
{1,2,4,2,3}
3
Output: []
Explanation:
The tree is look like this:
1
/ \
2 4
/ \
2 3
Notice we need to find all paths from root node to leaf nodes.
1 + 2 + 2 = 5, 1 + 2 + 3 = 6, 1 + 4 = 5
There is no one satisfying it.

思路:

递归法,用helper方法,可以传更多参数

注意:

代码:

/*
* @param root: the root of binary tree
* @param target: An integer
* @return: all valid paths
*/
public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
List<List<Integer>> result = new ArrayList<>();
ArrayList<Integer> path = new ArrayList<>(); if (root == null) {
return result;
}
path.add(root.val);
helper(root, path, root.val, target, result);
return result;
} public void helper (TreeNode root,
ArrayList<Integer> path,
int sum,
int target,
List<List<Integer>> result) {
//meet leaf
if (root.right == null && root.left == null) {
if (sum == target) {
result.add(new ArrayList(path));
}
return;
}
// right node
if (root.right != null) {
path.add(root.right.val);
helper(root.right, path, sum + root.right.val, target, result);
path.remove(path.size() - 1);
}
//left node
if (root.left != null) {
path.add(root.left.val);
helper(root.left, path, sum + root.left.val, target, result);
path.remove(path.size() - 1);
}
}

Lintcode376-Binary Tree Path Sum-Easy的更多相关文章

  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. 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 ...

  3. 376. Binary Tree Path Sum【LintCode java】

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

  4. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  5. 【leetcode】Minimum Path Sum(easy)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  6. [leetcode] #112 Path Sum (easy)

    原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...

  7. leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

  8. 113. Path Sum II (Tree; DFS)

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. 112. Path Sum (Tree; DFS)

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

随机推荐

  1. 1 主机WiFi连接下与虚拟机通信问题

    环境: 主机:win10系统 虚拟机软件:VMware 虚拟机:winserver 2012 R2 datacenter (数据中心版) 网络上有很多方法说设置NAT模式,并不好用,主机如果用网线连接 ...

  2. ajax里post 设置请求头的编码格式

    我们常用的ajax形式就是post和get.post需要设置请求头,那么问题来了: 首先,为什么get不需要设置编码格式? 其次:不设置post请求头编码格式可以吗? 还有:常用的请求头编码格式有哪些 ...

  3. python中文分词库——pynlpir

    安装 pip install pynlpir import pynlpir #加载包 pynlpir.open() #加载nlpir的库,这步是必须的 #否则会出现 段错误/段转储 segment f ...

  4. uploadify 火狐 http error:302

    网上查询了一都说是flash 中 Session问题 (IE会自动复制过去),大多说将session值传过去就可以了但我们用的是公司的一套权限,改不了用户登录信息 无奈只好不用用户信息来做,果然对了 ...

  5. css学习_css常见属性用法

    1.元素的显示模式 a.被动转换:浮动.绝对定位.固定定位(转换为行内块元素特性的模式---不设置宽度时,模式换行为行内块模式后宽度是内容宽度.) b.主动转换:display:block  / in ...

  6. ubuntu16.04安装libzip库

    sudo apt install libzip-dev

  7. ZOJ 4070 - Function and Function - [签到题][2018 ACM-ICPC Asia Qingdao Regional Problem M]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5840 Time Limit: 1 Second Mem ...

  8. JS常见的字符串操作

    1.charAt() 获取字符串指定位置的字符    用法:strObj是字符串对象,index是指定的位置,(位置从0开始数) strObj.charAt(index) 2. indexOf() 方 ...

  9. PE、PB、PEG三大估值法的正确使用方法!

    目前市面上的估值方法有很多,比如PE估值法.PB估值法.PEG估值法,但是我相信,真正会用的人并不多,比如说目前动态市盈率121倍的比亚迪真的高估吗?比如目前市净率为0.63倍的众泰汽车真的是破净股吗 ...

  10. 遇到NotificationService: Suppressing notification from package com.example.dell.servicebestpractice by u错误

    一般来说是手机设置没有允许通知