You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

Example:

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1 Return 3. The paths that sum to 8 are: 1. 5 -> 3
2. 5 -> 2 -> 1
3. -3 -> 11 这个题目的思路就是类似于[LeetCode] 113. Path Sum II, 只不过我们不需要一定是leaf再判断, 同时recursive root.left and root.right, 最后返回总的个数即可. 1. Constraints
1) empty => 0 2. IDeas DFS T: O(n^2) worst cases, when like linked lists
T: O(nlgn) best cases, when balanced tree because height = lgn 1) edge case, if not root: return 0
2) create a helper function, get number of paths from root -> any child node s.t sum(path) == target
3) return helper(root) and recursively call root.left and root.right 3. Code
 class Solution:
def pathSum3(self, root, target):
def rootSum(root, target): # helper function to get number of paths from root -> any child node s.t sum == target
if not root: return 0
d = target - root.val
temp = 1 if d == 0 else 0
return temp + rootSum(root.left, d) + rootSum(root.right, d)
if not root: return 0
return rootSum(root, target) + self.pathSum3(root.left, target) + self.pathSum3(root.right, target)

4. Test cases

1) empty

2) 1, 1

3)

1
/ \
1 1
target = 1
4)
target = 8
      10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1

[LeetCode] 437. Path Sum III_ Easy tag: DFS的更多相关文章

  1. 47. leetcode 437. Path Sum III

    437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...

  2. [LeetCode] 437. Path Sum III 路径和 III

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

  3. LeetCode 437. Path Sum III (路径之和之三)

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

  4. leetcode 437 Path Sum III 路径和

      相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...

  5. Leetcode 437. Path Sum III

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

  6. [LeetCode] 257. Binary Tree Paths_ Easy tag: DFS

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  7. LeetCode 437. Path Sum III (STL map前缀和)

    找遍所有路径,特判以根为起点的串即可. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...

  8. [LeetCode] 112. Path Sum 路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  9. [LeetCode] 113. Path Sum II 路径和 II

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

随机推荐

  1. Android手机资料拷贝导出工具 --- 91手机助手

    http://zs.91.com/

  2. Cmd find命令 和 findstr 命令

    https://blog.csdn.net/icanlove/article/details/37567591   Windows CMD中 find命令(字符串查找) https://blog.cs ...

  3. Storm启动流程分析

    1. 客户端运行storm nimbus时,会调用storm的python脚本,该脚本中为每个命令编写一个方法,每个方法都可以生成一条相应的java命令. 命令格式如下:java -server xx ...

  4. mac Intellij Idea Tmocat 启动报 Error running Tomcat: /conf/Catalina

    原因:主要是tomcat下Catalina目录没有权限导致,将其设置读写权限即可 如果在刚刚启动tomcat时出现以下问题:Error running Tomcat 8.5.31: Error cop ...

  5. sencha touch Container

    Container控件是我们在实际开发中最常用的控件,大部分视图控件都是继承于Container控件,了解此控件能帮我们更好的了解sencha touch. layout是一个很重要的属性,能够帮助你 ...

  6. SDRAM相位角计算

    SDRAM相位角计算 下面是我复制别人的没有图片 如果想看原文 点击下面链接,, http://wenku.baidu.com/view/91e2d76a27284b73f24250e6.html 一 ...

  7. iOS - UIScreen的 bound、frame、scale属性

    A UIScreen object contains the bounding rectangle of the device’s entire screen. When setting up you ...

  8. spark脚本日志输出级别设置

    import org.apache.log4j.{ Level, Logger } Logger.getLogger("org").setLevel(Level.WARN) Log ...

  9. mysql语句性能分析

    1.开启慢查询 slow_query_log = 1 //开启 slow_query_log_file = mysql_slow_query.log //日志文件位置 long_query_time ...

  10. ThinkPHP widge使用示例

    1.widge一般用于公用模块的设计与使用,以便加强软件模块的复用性与重用性 一般使用include方法设计公共模块,比如<include file="home:header" ...