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. 【PHP】使用GD库实现 图像生成、缩放、logo水印和简单验证码

    gd库是php最常用的图片处理库之一(另外一个是imagemagick),可以生成图片.验证码.水印.缩略图等等.要使用gd库首先需要开启gd库扩展, windows系统下需要在php.ini中将ex ...

  2. try except与try finally不同之处

    try except与try finally不同之处 try//尝试执行 {SomeCode}  except//出错的时候执行, Except有特定的错误类型  {SomeCode}  end; t ...

  3. 使用MSBUILD 构建时出错 error MSB3086: 任务未能使用 SdkToolsPath“”或注册表项“XXX”找到“LC.exe”,请确保已设置 SdkToolsPath。

    如果项目有添加有WB引用,比如引用其它网站的WEB服务等,那么VS在编译时会自动生成个 [项目名称].Serializers.dll的文件,就是把引用服务中的相关对象信息生成硬编码的程序集,以提高效率 ...

  4. Elasticsearch 学习之配置文件详解

    Elasticsearch配置文件##################### Elasticsearch Configuration Example ##################### # # ...

  5. Oracle 学习之 Select 1

    1. select 1 from table           增加临时列,每行的列值是写在select后的数,这条sql语句中是1,若select后为2,则是每行为2的列 2:select cou ...

  6. linux下的一些操作命令

    1.切换到root账号下: su root    输入密码: 2.修改root账号密码: sudo passwd root   输入密码: 3.cat用法: 查看文件内容   cat 文件名 创建文件 ...

  7. 2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)

    Plants vs. Zombies Time Limit: 2 Seconds      Memory Limit: 65536 KB BaoBao and DreamGrid are playin ...

  8. Python 安装出错:Setup script exited with error: command 'gcc' failed with exit status 1

    退出当前环境: logout (再重新登录进去) yum install python-devel  -yyum install libevent-devel  -y 把环境更新下yum instal ...

  9. Egret IDE中搜索,过滤文件,只搜索.ts

    刚开始忘了这个搜索条件在哪里打开了,后来找着了,记录一下 = =!

  10. 获取Web.config的内容

    <web.config> web.config文件是一个XML文件,它的根结点是<configuration>,在<configuration>节点下的常见子节点有 ...