Path Sum II

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

For example:
Given the below binary tree and sum = 22,

              5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

return

[
[5,4,11,2],
[5,8,4,5]
] 这个问题需要返回每条满足sum值的所有路径。思路是:后序遍历,回溯时将当前node.val添加到左右子树的结果中。 递归:
List<List<Int>> path_sum_helper(Treenode node, int current_sum, int depth, int sum_aim):
  if (node is leaf-node):
    r = [[]]
    if (current_sum == sum_aim):
      r_i = []
      r_i[depth] = node.val
      r.add(r_i)
    return r
  else:
    r = [[]]
    if(node.left != null):
      left_ = path_sum_helper(node.left, current_sum + node.left.val, depth + 1, sum_aim)
      if (left_ != null):
        r = left_;
        for(r_ : r):
          r_[level] = node.val
      // similar for right sub-tree
      // need to merge left and right results
    return r
      


[leetcode]Path Sum II的更多相关文章

  1. LeetCode: Path Sum II 解题报告

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  2. [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 ...

  3. [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 ...

  4. [leetcode]Path Sum II @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...

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

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

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

  8. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  9. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

随机推荐

  1. HTML代码简写法:Emmet和Haml

    http://www.ruanyifeng.com/blog/2013/06/emmet_and_haml.html?bsh_bid=657901854 HTML代码简写法:Emmet和Haml   ...

  2. [DNS-BIND]网络初始化

    1.创建ns_g_socketmgr: 首先,套接字管理器是全局唯一的,与有多少个网络接口无关,全局变量定义在/bin/named/include/named/globals.h: EXTERN is ...

  3. C语言深度学习——第一天

    首先声明一下,在我们写的程序中,会使用到一个头文件# include <head.h> 因为,在linux系统编程的时候,会用到很多头文件,为此,我用一个头文件全部包含在一起,头文件内容如 ...

  4. Win Form程序线程点点

    消息循环 Win32窗体程序基于消息驱动的,程序的模型就是一个用户触发事件消息->系统分发事件消息->程序处理事件的循环过程. .NET Win Form程序对消息循环进行了封装,可以看到 ...

  5. 常用js字符串方法学习总结

    2016-06-15 js数组和字符串方法有很多,并且有一部分在使用的过程中有很多方法是很容易被混淆的,今天来总结一下js中数组和字符串的方法. ♦数组(Array)的方法 1.push() 和 po ...

  6. __autoload的小tip

    __autoload魔法函数不仅在如 $a=new testClass();时可以触发.在 class a extends b时 如果b类的定义在当前页未找到,也可以触发这个函数

  7. 第十一&十二&十三周周记

    周数 专业学习目标 专业学习时间 新增代码量 博客发表量 人文方面的学习 知识技能总结 第十一周 认真学习网络技术,了解路由器和交换机之间的联通和使用. 一天一小时 300 一篇 每天用一小时看关于经 ...

  8. iOS socket保持后台连接 ios9.0 xcode8.0

    可以保持后台,但申请上架是肯定会被拒的 本教程是基于AsyncSocket库的简单开发! socket机制今天就不说了,毕竟百度上太多太详尽了! 1.先new一个工程: ​2.要写socket的界面遵 ...

  9. Linux内核分析——进程描述与创建

    20135125陈智威 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验内容 ...

  10. Tips for VNCServer config

    Tips for VNCServer After the ClearCase server reboot by Jingwei, my vncserver background process is ...