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.
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
*/
public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
// Write your code here
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (root == null){
return res;
}
List<Integer> path = new ArrayList<Integer>();
helper(res, path, root, target);
return res;
} private void helper(List<List<Integer>> res, List<Integer> path, TreeNode root, int target){
if (root.left == null && root.right == null){
if (root.val == target) {
path.add(root.val);
res.add(new ArrayList<Integer>(path));
path.remove(path.size() - 1);
}
return;
}
path.add(root.val);
if (root.left != null) {
helper(res, path, root.left, target - root.val);
}
if (root.right != null) {
helper(res, path, root.right, target - root.val);
}
path.remove(path.size() - 1);
return;
}
}
Binary Tree Path Sum的更多相关文章
- [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 ...
- 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 ...
- 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 equa ...
- 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 ...
- Path Sum II (Find Path in Tree) -- LeetCode
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
随机推荐
- git help 机器翻译
该篇发布仅为博主个人保存并参考,内容可能不对 usage: git [--version] [--help] [-C <path>] [-c <name>=<value& ...
- Linux VNC server 安装配置
1.安装vnc server [root@pxe ~]# yum install tigervnc-server -y 2.设置 vnc server 开机启动 [root@pxe ~]# chk ...
- Porsche Piwis Tester II V12.100 Version Released
Piwis Tester II v12.100 Version released today! In this new version we can find the latest type Pors ...
- Electron把网页打包成桌面应用并进行源码加密
前言 最近想把自己用html+css+js做的网页界面打包成桌面应用,网上一搜,发现Electron是一个不错的选择,试了试,发现效果真的不错.这里记录一下打包过程以作记录,便于自己以后查看学习. 一 ...
- UUID简介
UUID简介如下:1.简介UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software F ...
- Python RabbitMQ 权重设置
消费端recv设置 注:设置消费端处理完一条消息后再发另一条 channel.basic_qos(prefetch_count=1) 由于每一条机器的处理速度不同,所以我们这里就会对应,机 ...
- opencv学习之路(26)、轮廓查找与绘制(五)——最小外接矩形
一.简介 二.轮廓最小外接矩形的绘制 #include "opencv2/opencv.hpp" using namespace cv; void main() { //轮廓最小外 ...
- springboot日志配置
默认情况下,spring boot使用的是LogBack日志系统.在spring-boot-starter-web和spring-boot-starter中都已经默认依赖了logging的工具包. 如 ...
- (转载)Unity 关于动态监听时,点击Button,返回其在数组中的下标
其实是绕了一圈,把数组里的元素放进数组列表里再读取它的下标 using System.Collections; using System.Collections.Generic; using Unit ...
- .NET Core 管道过滤器扩展
if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseEx ...