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 ...
随机推荐
- [openjudge-贪心]删数问题
题目描述 题目描述 键盘输入一个高精度的正整数N,去掉其中任意k个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对给定的N和k,寻找一种方案使得剩下的数字组成的新数最小. 输出应包括所去掉的数 ...
- Android-Gradle(一)
理解基本的Gradle 如果你想创建一个Android project基于gradle,那么你必须写一个构建脚本,这个文件通常称之为build.grade,你可能已经觉察到了,当我们查看这一脚本,gr ...
- Hdu2015 偶数求和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2015 偶数求和 Time Limit: 2000/1000 MS (Java/Others) M ...
- element table 二次封装 父子组件传值 组件通信
新建一个组件(即子组件)table.vue 子组件编辑内容如下图所示 子组件通过props获取父组件传递过来的参数,如下图所示,type指明传递到子组件的数据类型,default指定默认值,一般不给 ...
- ProcessExplorer使用分享
工具描述 Process Explorer使用个轻量级的进程管理器,是由Sysinternals出品的免费工具,请猛击这里下载最新版本使用. 以下是官方介绍的翻译: “想知道是那个程序打开了某个文件或 ...
- Link-Cut-Tree详解
图片参考YangZhe的论文,FlashHu大佬的博客 Link-Cut-Tree实际靠的是实链剖分,重链剖分和长链剖分珂以参考树链剖分详解 Link-Cut-Tree将某一个儿子的连边划分为实边,而 ...
- 使用Angular2的Http发送AJAX请求
使用Angular2的Http发送AJAX请求 Angular的文档并不详细,甚至API文档也有一些错误.经过查阅资料并经大量实验,终于明确了Angular的Http发送Ajax请求的方式方法.本文描 ...
- POI使用记录
POI CellType类型 CellType 类型 值 CELL_TYPE_NUMERIC 数值型 0 CELL_TYPE_STRING 字符串型 1 CELL_TYPE_FORMULA ...
- flutter登录页部分内容
import 'package:flutter/material.dart'; class MyIdPage extends StatelessWidget { @override Widget bu ...
- makefile中的patsubst函数有何作用?
答:这是个模式替换函数,格式为: $(patsubst <pattern>,<replacement>,<text>) 查找text中的单词,如果匹配pattern ...