早上看到一个面经题跟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. bash shell漏洞及测试

    1.bash shell是大多数linux发行版本的默认shell命令解释器,但是最近爆出bash shell存在漏洞. 2.如果Bash是默认的系统shell,网络攻击者可以通过发送Web请求.se ...

  2. SqlServer正在执行的sql语句

    SELECT [Spid] = session_Id ,ecid ,[Database] = DB_NAME(sp.dbid) ,[User] = nt_username ,[Status] = er ...

  3. Android -- 创建桌面快捷方式

    代码                                                                                    /** * * 返回添加到桌 ...

  4. [转载]char * 和char []的区别---之第二篇

    原文地址:http://blog.sina.com.cn/s/blog_74a4593801019keb.html main() { char *p="abc123ABC";//c ...

  5. Incorrect string value: '\xF0\xA1\xA1\x92' for column 'herst' at row 1

    Incorrect string value: '\xF0\xA1\xA1\x92' for column 'herst' at row 1[转] 1.一般来说MySQL(小于5.5.3)字符集设置为 ...

  6. Python 删除列表中的重复数据

    list0=['b','c', 'd','b','c','a','a'] 方法1:使用set() list1=sorted(set(list0),key=list0.index) # sorted o ...

  7. JS设计模式——5.单体模式

    JS设计模式——5.单体模式 http://www.cnblogs.com/JChen666/p/3610585.html   单体模式的优势 用了这么久的单体模式,竟全然不知!用它具体有哪些好处呢? ...

  8. Hadoop分布式配置

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 请先参照Linux安 ...

  9. 8086CPU各寄存器的用途

    8086 有14个16位寄存器,这14个寄存器按其用途可分为(1)通用寄存器.(2)指令指针.(3)标志寄存器和(4)段寄存器等4类. 1.通用寄存器有8个, 又可以分成2组,一组是数据寄存器(4个) ...

  10. mybatis中:returned more than one row, where no more than one was expected.异常

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorEx ...