Skip to content

Commit f52665e

Browse files
committed
437
1 parent 04cec55 commit f52665e

File tree

4 files changed

+91
-16
lines changed

4 files changed

+91
-16
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
9292
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/#/description)| [Python [Yu]](./tree/Yu/226.py) | _O(N)_| _O(1)_ | Easy | |[公瑾讲解](https://youtu.be/oiX3mqcAK0s)|
9393
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/#/description)| [Python [Yu]](./tree/Yu/543.py) | _O(N)_| _O(h)_ | Easy | |[公瑾讲解](https://www.youtube.com/watch?v=0VnOfu2pYTo)|
9494
|501|[Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree/#/description)| [Python [Yu]](./tree/Yu/501.py) | _O(N)_| _O(N)_ | Easy | |[公瑾讲解](https://youtu.be/v4F4x_uwMb8)|
95+
|572|[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/#/description)| [Python [Yu]](./tree/Yu/572.py) | _O(S*T)_| _O(1)_ | Easy | |[公瑾讲解](https://youtu.be/v4F4x_uwMb8)|
96+
|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii/#/description)| [Python [Yu]](./tree/Yu/437.py) | _O(N^2)_| _O(1)_ | Easy | |[公瑾讲解](https://youtu.be/NTyOEYYyv-o)|
97+
9598

9699
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/#/description)| [Python [Yu]](./tree/Yu/563.py) | _O(N)_| _O(1)_ | Easy | |[公瑾讲解](https://youtu.be/47FQVP4ynk0)|
97100
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/#/description)| [Python [Yu]](./tree/Yu/257.py) | _O(N)_| _O(N)_ | Easy | |[公瑾讲解](https://youtu.be/Zr_7qq2f16k)|

tree/Yu/102.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,24 @@
1111

1212

1313
class Solution(object):
14-
#def __init__(self):
15-
# self.res = []
16-
1714
def levelOrder(self, root):
1815
"""
1916
:type root: TreeNode
2017
:rtype: List[List[int]]
2118
"""
22-
res = []
23-
self.dfs_level(root, 0, res)
24-
return res
19+
self.res = []
20+
def dfs(root, level):
21+
# Edge
22+
if not root:
23+
return []
24+
# Process
25+
if level >= len(self.res):
26+
self.res.append([])
27+
self.res[level].append(root.val)
28+
29+
# Recursion
30+
dfs(root.left, level + 1)
31+
dfs(root.right, level + 1)
2532

26-
def dfs_level(self, node, level, res):
27-
#Edge
28-
if not node:
29-
return
30-
#res: [[3],[9, 20], [15,7 ]]
31-
if level >= len(res):
32-
res.append([])
33-
res[level].append(node.val)
34-
self.dfs_level(node.left, level+1, res)
35-
self.dfs_level(node.right, level+1, res)
33+
dfs(root, 0)
34+
return self.res

tree/Yu/437.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Author: Yu Zhou
5+
# 437. Path Sum III
6+
7+
# Definition for a binary tree node.
8+
# class TreeNode(object):
9+
# def __init__(self, x):
10+
# self.val = x
11+
# self.left = None
12+
# self.right = None
13+
14+
class Solution(object):
15+
def pathSum(self, root, sum):
16+
"""
17+
:type root: TreeNode
18+
:type sum: int
19+
:rtype: int
20+
"""
21+
22+
# Edge Case:
23+
if not root:
24+
return 0
25+
26+
# Process
27+
def dfs(root, sum):
28+
count = 0
29+
30+
if not root:
31+
return 0
32+
if root.val == sum:
33+
count += 1
34+
35+
count += dfs(root.left, sum - root.val)
36+
count += dfs(root.right, sum -root.val)
37+
return count
38+
39+
# Recursion
40+
return dfs(root,sum) + self.pathSum(root.left, sum) + self.pathSum(root.right, sum)

tree/Yu/572.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Author: Yu Zhou
5+
# 572. Subtree of Another Tree
6+
7+
# ****************
8+
# Descrption:
9+
# ****************
10+
11+
class Solution(object):
12+
def isSubtree(self, s, t):
13+
"""
14+
:type s: TreeNode
15+
:type t: TreeNode
16+
:rtype: bool
17+
"""
18+
#Edge
19+
if not s or not t:
20+
return False
21+
22+
#Process
23+
if self.isMatch(s,t):
24+
return True
25+
26+
#Recusion
27+
return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)
28+
29+
def isMatch(self, s, t):
30+
if not s or not t:
31+
return s == t
32+
33+
return s.val == t.val and self.isMatch(s.left, t.left) and self.isMatch(s.right, t.right)

0 commit comments

Comments
 (0)