Lintcode376-Binary Tree Path Sum-Easy
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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- 【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 ...
- [leetcode] #112 Path Sum (easy)
原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & 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 ...
- 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 ...
- 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 ...
随机推荐
- SHELL编程之产生随机数
shell有一个环境变量RANDOM,范围是0-32767 如果想得到1-68范围内的数:$(($RANDOM%68+1)) 或者创建随机数函数: function rand() { min=$1 m ...
- mysql 查询排名 返回值拼接的骚操作
故事背景: 依照原系统的框子搞一个新的系统给其他人使用,因为新的系统配置库依然需要使用原有的表,表中有字段区分新的系统,然后就有了这个很没劲的数据同步. 难点:配置库码表数据的主键之前是自增,后来改造 ...
- 剑指offer——python【第44题】翻转单词顺序
题目描述 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,“student ...
- c++课设
#include <stdio.h>#include <time.h>#include <math.h>#define C 60000;struct Student ...
- ERP项目实施记录09
今天报价软件测试版本出来了,可看上去不怎么像是一款报价的软件,整个界面上都没有"报价"相关的字眼: 软件标题就不说了,反正影响不大,就当没看见,可左边这一大片菜单里也找不到和报价有 ...
- SIM800C 连接服务器
AT+CIPSTART=TCP,域名,端口号 OK 只返回OK,这种情况,说明域名的服务器出错了,OK表示格式正确,但是实际上的TCP是没有连接上的. 测试库服务器出错的时候,就是这种情况 实际连上了 ...
- Web开发——HTML基础
文档资料参考: 参考:MDN官网 参考:http://www.runoob.com,W3School 参考:https://developer.mozilla.org/zh-CN/docs/Learn ...
- 【摘】Fiddler工具使用介绍
摘自:https://www.cnblogs.com/miantest/p/7289694.html Fiddler基础知识 Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作 ...
- 解决Kettle ETL数据乱码
首先用insert语句插入一条数据试试是否因为MySQL编码不对引起,如果是MySQL原因,修改MySQL编码即可: 如果不是因为MySQL的编码导致问题,那么在Kettle的表输出中,编辑连接-选项 ...
- Redis入门到高可用(十五)—— HyperLogLog
一.简介 二.API Demo 三.使用经验