早上看到一个面经题跟Path Sum很像, 给一个TreeNode root和一个target,找到一条从根节点到leaf的路径,其中每个节点和等于target。 与Path Sum不同是, Path Sum要求返回boolean,这道稍作改动返回路径。原理都一样

public class Solution {
public List<Integer> getPathSum(TreeNode root, int target) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
getPathSum(res, root, target);
return res;
} private boolean getPathSum(List<Integer> res, TreeNode root, int target) {
if (root == null) {
return false;
}
res.add(root.val);
target -= root.val;
if (root.left == null && root.right == null && target == 0) {
return true;
}
if (getPathSum(res, root.left, target)) {
return true;
}
if (getPathSum(res, root.right, target)) {
return true;
}
res.remove(res.size() - 1);
return false;
}
}

Path Sum的变体的更多相关文章

  1. 二叉树中的最大路径和 · Binary Tree Maximum Path Sum

    [抄题]: 给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和) [思维问题]: 不会写分合法 [一句话思路]: 用两次分治:ro ...

  2. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  3. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  4. [LeetCode] 666. Path Sum IV 二叉树的路径和 IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  5. 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 ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  9. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

随机推荐

  1. c++ goto的使用

    c++ goto 语句的使用 1.定义一个类似标签的东西lable 2.使用goto关键字,跳转到lable, goto lable #include <iostream> #includ ...

  2. 卷积神经网络CNN介绍:结构框架,源码理解【转】

    1. 卷积神经网络结构 卷积神经网络是一个多层的神经网络,每层都是一个变换(映射),常用卷积convention变换和pooling池化变换,每种变换都是对输入数据的一种处理,是输入特征的另一种特征表 ...

  3. multipart/form-data

    Content-Type的类型扩充了multipart/form-data用以支持向服务器发送二进制数据

  4. 使用sqlserver日期函数获取当前日期

    使用sqlserver日期函数中的getdate()可以获取当现的日期,下面就将为您介绍这种使用sqlserver日期函数获取当前日期的方法,供您参考,希望对您学习sqlserver日期函数有所启迪. ...

  5. GB28181国检推流

    GB28181国检有一向内容是实时播放摄像机流,经过一番努力,搞定这个功能,现分享心得: 首先需要了解流程,说简答点就是视频流从哪里来到什么地方去,下图描述了视频流推流,转发的 基本过程:信令交互成功 ...

  6. 【转】利用TCMalloc优化Nginx的性能

    From: http://www.linuxidc.com/Linux/2013-04/83197.html TCMalloc的全称是 Thread-Caching Malloc,是谷歌开发的开源工具 ...

  7. hibernate--OneToOne

    一对一(one to one) 单向关联映射 两个对象是一对一的的关系. 有两种策略可以实现一对一的关联映射 l  主键关联:即让两个对象具有相同的主键值,以表明他们之间的一对一的对应关系;数据库表不 ...

  8. linux I/O

    一) I/O调度程序的总结     1) 当向设备写入数据块或是从设备读出数据块时,请求都被安置在一个队列中等待完成.     2) 每个块设备都有它自己的队列.     3) I/O调度程序负责维护 ...

  9. 【CentOS】IBM X3650M4 IMM远程管理【转载】

    问题描述:          IBM X3650M4 IMM远程开机和关机   参考资料:             http://www.ibmsys.cn/blog/?p=201   问题解决: 一 ...

  10. LAMP环境的搭建

    [一些前言废话]一名web开发尤其是后端不懂LAMP环境的搭建,那就摊上事了,有些人说他一直用win下的wampServer这种傻瓜式环境搭建,用的挺好的,也有人说他用云服务器,搭配“一键搭建LAMP ...