[LeetCode] 437. Path Sum III_ Easy tag: DFS
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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- leetcode 437 Path Sum III 路径和
相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
- 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 ...
- [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 ...
- LeetCode 437. Path Sum III (STL map前缀和)
找遍所有路径,特判以根为起点的串即可. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...
- [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 ...
- [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 ...
随机推荐
- 动力学仿真引擎ODE的学习笔记,C#演示(一)
®版权声明:本文为博主原创文章,未经博主允许不得转载. 一.ODE介绍与平台搭建. 接触到动力学仿真引擎, 是因为笔者的一款PLC仿真软件需要3D仿真.我需要达到的效果是,以3D方式构建出工控行业中常 ...
- LeetCode 42 Trapping Rain Water(积水体积)
题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意 ...
- 12个常用的JavaScript技巧
在这篇文章中将给大家分享12个有关于JavaScript的小技巧.这些小技巧可能在你的实际工作中或许能帮助你解决一些问题. 使用!!操作符转换布尔值 有时候我们需要对一个变量查检其是否存在或者检查值是 ...
- 网站测速、ping
1.17ce 2. 360奇云测 3.http://ping.chinaz.com/ 效果图:
- 静态时序分析基础STA
静态时序分析SAT 1. 背景 静态时序分析的前提就是设计者先提出要求,然后时序分析工具才会根据特定的时序模型进行分析,给出正确是时序报告. 进行静态时序分析,主要目的就是为了提高系统工作主频 ...
- 【CF884F】Anti-Palindromize 费用流
[CF884F]Anti-Palindromize 题意:定义一个串是反回文的,当且仅当对于1<=i<=len,$a_i!=a_{len-i+1}$. 现在给出一个长度为n的串S(n是偶数 ...
- 【CF896D】Nephren Runs a Cinema 卡特兰数+组合数+CRT
[CF896D]Nephren Runs a Cinema 题意:一个序列中有n格数,每个数可能是0,1,-1,如果一个序列的所有前缀和都>=0且总和$\in [L,R]$,那么我们称这个序列是 ...
- eclipse打断点只进入class文件中的解决办法
内容来源 https://www.cnblogs.com/scode2/p/8671908.html#undefined 是由于对应的Java类跟编译后的class文件,没有关联上, 解决办法: 在打 ...
- vmware新建Ubuntu时,提示此主机不支持 Intel VT-x
有两种解决方式 一.BIOS中打开CPU虚拟选项,不同厂商主板配置不同: 以下以个人thinkpad T460P电脑为例: 1.关机,开机,在启动时,按F1今天 BIOS 设置页面: 2.选择 Sec ...
- postgresql----文本搜索类型和检索函数
postgresql提供两种数据类型用于支持全文检索:tsvector类型产生一个文档(以优化全文检索形式)和tsquery类型用于查询检索. tsvector的值是一个无重复的lexemes排序列表 ...