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 ...
随机推荐
- 16-1 ECMA5与ECMA6的函数定义
ECMA5: function Drag(id){ this.ele = document.getElementById(id); var that = this; this.ele.onmoused ...
- SHELL编程之产生随机数
shell有一个环境变量RANDOM,范围是0-32767 如果想得到1-68范围内的数:$(($RANDOM%68+1)) 或者创建随机数函数: function rand() { min=$1 m ...
- SSH免密码登录配置
ssh免密码登录Permission denied (publickey,gssapi-keyex,gssapi-with-mic) 的解决方案 1.在hadoop目录 新建.ssh目录 使用:ssh ...
- 补充:ajax PHP html js 实现 三级联动(省 市 区)
html + js 在一个页面 php
- 微信OAuth授权获取用户OpenId-JAVA(个人经验)【申明:来源于网络】
微信OAuth授权获取用户OpenId-JAVA(个人经验)[申明:来源于网络] 地址:https://my.oschina.net/xshuai/blog/293458
- php利用OpenXML规范生成word,excel(pdf其他方法)
这个涉及到的东西比较多 HTTP MIME types $contract_data = '<html xmlns:o="urn:schemas-microsoft-com:offic ...
- ubuntu下安装thrift
configure: error: "Error: libcrypto required."
- Spark Streaming实战演练
一.spark streaming简介 Streaming是一种数据传输技术,它把客户机收到的数据变成一个稳定连续的流,源源不断的输出,使用户听到的声音和图像十分稳定,而用户在整个文件传输完成开始前就 ...
- 【RabbitMQ】工作模式介绍
一.前言 之前,笔者写过< CentOS 7.2 安装 RabbitMQ> 这篇文章,今天整理一下 RabbitMQ 相关的笔记便于以后复习. 二.模式介绍 在 RabbitMQ 官网上提 ...
- CRM项目自定义的知识点
python manage.py shell #自动配置环境 a = models.CustomerInfo #实例对象可以a._meta # dir 可以查看字段方法 a._meta.app_lab ...