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 ...
随机推荐
- C# 6.0 的?.运算符
What is it? Here’s the scenario Consider getting the grandchild of a parent object like this: var g1 ...
- jsp内置对象pageContext如何在Servlet中获取值
pageContext javax.servlet.jsp.PageContext 的实例,该对象代表该JSP 页面上下文,使用该对象可以访问页面中的共享数据.常用的方法有getServletCont ...
- JS异步编程 (2) - Promise、Generator、async/await
JS异步编程 (2) - Promise.Generator.async/await 上篇文章我们讲了下JS异步编程的相关知识,比如什么是异步,为什么要使用异步编程以及在浏览器中JS如何实现异步的.最 ...
- 协议类接口 - SPI
一.SPI概述 SPI(Serial Peripheral Interface,串行外设接口)总线系统是一种同步串行外设接口,它可以使CPU与各种外围设备以串行方式进行通信以交换信息.一般主控SoC ...
- sudo命令: 在其他用户下操作root用户权限
一. 场景: 在某个远程服务器 A 上,用 账户1 登陆, 想要在root用户的目录下创建一个 .sh文件, 如果直接 用 touch test.sh 创建,会提示权限不足 此时可以用sudo命令: ...
- ios之coredata(一)
下面开始学习一下CoreData. Core Data不是一个关系型数据库,也不是关系型数据库管理系统(RDBMS).Core Data 为数据变更管理.对象存储.对象读取恢复的功能提供了支持. 它可 ...
- WPF几个样式
其实也是大家学的最多的,网上的. 1.老版360 2.360悬浮窗 不好意思,没有找到悬浮球的图片,随便一个代替了 3.老版迅雷 4.新版360 遗憾的是这个样式没有完整的源代码.只是一个演示和图片代 ...
- vue的$emit 与$on父子组件与兄弟组件的之间通信
本文主要对vue 用$emit 与 $on 来进行组件之间的数据传输. 主要的传输方式有三种: 1.父组件到子组件通信 2.子组件到父组件的通信 3.兄弟组件之间的通信 一.父组件传值给子组件 父组件 ...
- keepalived+nginx+tomcat+redis实现负载均衡和session共享(原创)
keepalived+nginx+tomcat+redis实现负载均衡和session共享 直接上链接,码了一天,就不再重写了,希望能帮到大家,有问题欢迎留言交流.
- MySQL---视图、触发器
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( S ...