早上看到一个面经题跟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. 微软职位内部推荐-Senior Software Engineer-Office Incubation

    微软近期Open的职位: Office China team is looking for experienced engineers to improve consumer experience i ...

  2. 20145103《java程序设计》第4周学习总结

    20145103 <Java程序设计>第4周学习总结 教材学习内容总结 继承 继承共同行为 ·继承基本上就是避免多个类间重复定义共同行为. ·继承的三个好处:减少代码冗余:维护变得简单:扩 ...

  3. [转载]Nginx如何处理一个请求

    http://nginx.org/cn/docs/http/request_processing.html 对我的扫盲文章 基于名字的虚拟主机 Nginx首先选定由哪一个虚拟主机来处理请求.让我们从一 ...

  4. Java_Swing实现小球沿正弦曲线运动的代码

    1 package zuidaimapack_1; import java.awt.*; import javax.swing.*; /** *Java_Swing实现小球沿正弦曲线运动的代码 * @ ...

  5. Android开发-环境搭建以及HelloWorld

    最近开始进行Android的开发,没有基础完全从0开始.   首先,知道Android开发的官方网站: http://developer.android.com/index.html 网站本身教程非常 ...

  6. 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  7. C实现面向对象封装、继承、多态

    参考资料:      http://blog.chinaunix.net/uid-26750235-id-3102371.html      http://www.eventhelix.com/rea ...

  8. Matlab求范数

    对 p = 2,这称为弗罗贝尼乌斯范数(Frobenius norm)或希尔伯特-施密特范数( Hilbert–Schmidt norm),不过后面这个术语通常只用于希尔伯特空间.这个范数可用不同的方 ...

  9. Leetcode#150 Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

  10. Linux --windows vs

    我其实并不是很清楚我在做什么....希望做完之后可以解答....... 在看了一堆GNU, Clang, GCC, QT, MinGW, CygWin, POSIX 这些概念之后,我觉得我在做的事情就 ...