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 number target.
A valid path is from root node to any of the leaf nodes.
Example
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
*/
int target = 0;
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
// write your code here
if(root == null){
return res;
}
this.target = target;
List<Integer>path = new ArrayList<>();
helper(path, root, 0);
return res;
}
public void helper(List<Integer>path, TreeNode root, int sum){
sum +=root.val;
path.add(root.val);
//如果到叶子结点了,并且和为target
if(root.left == null && root.right == null && sum == target){
ArrayList<Integer>temp = new ArrayList<>();
temp.addAll(path);//复制一份
res.add(temp);
}
if(root.left != null){
helper(path, root.left, sum);
}
if(root.right != null){
helper(path, root.right, sum);
}
path.remove(path.size() - 1);
}
}
376. Binary Tree Path Sum【LintCode java】的更多相关文章
- [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 ...
- 375. Clone Binary Tree【LintCode java】
Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...
- 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 ...
- leetcode:Path Sum【Python版】
1.类中递归调用函数需要加self # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # s ...
- 245. Subtree【LintCode java】
Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
- 365. Count 1 in Binary【LintCode java】
Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
随机推荐
- 【luogu P2491 [SDOI2011]消防】 题解
题目链接:https://www.luogu.org/problemnew/show/P2491 题外话: OI一共只有三种题--会的题,不会的题,二分题. 题解: step 1 求树的直径,把树的直 ...
- 移动端web开发技巧和常见问题
常见问题 1.移动端如何定义字体font-family 三大手机系统的字体: ios 系统 默认中文字体是Heiti SC 默认英文字体是Helvetica 默认数字字体是HelveticaNeue ...
- HDU 2021 发工资咯:)(最水贪心)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2021 发工资咯:) Time Limit: 2000/1000 MS (Java/Others) ...
- React Native IOS ---基础环境搭建(前端架构师)
React Native -IOS 开发环境搭建 web架构(基础) 安装依赖 * 必须安装的依赖有:Node.Watchman 和 React Native 命令行工具以及 Xcode. npm 镜 ...
- linux下安装rar
1. sudo wget https://www.rarlab.com/rar/rarlinux-x64-5.5.0.tar.gz 2.解压 tar -zxf rarlinux-5.0.1.tar.g ...
- 【HDOJ 2888】Check Corners(裸二维RMQ)
Problem Description Paul draw a big m*n matrix A last month, whose entries Ai,j are all integer numb ...
- ubnutu 安装protocol buffer
工作中需要使用protocol buffer,需要编译出protocol buffer的动态链接库,然后在别的makefile中链接它, 我的环境是ubnutu16.04,64bit,使用的proto ...
- Plugin 'InnoDB' registration as a STORAGE ENGINE failed
今天在安装mysql时遇到了mysql服务打不开的的情况,通过在cmd中输入MySQL --console,显示错误信息,得到如下情况. 原因是InnoDB初始化异常,也就是是说,卸载mysql的时候 ...
- MySQL必知必会 读书笔记三:检索数据和数据排序
检索数据 SELECT语句 它的用途是从一个或多个表中检索信息. 为了使用SELECT检索表数据,必须至少给出两条信息--想选择什 么,以及从什么地方选择. 检索单个列 SELECT col_1 FR ...
- sample采样倾斜key并单独进行join代码
/** * sample采样倾斜key单独进行join */ JavaPairRDD<Long, String> sampledRDD = userid2PartAggrInfoRDD.s ...