Skip to content

Commit 757c13d

Browse files
committed
270
1 parent f52665e commit 757c13d

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
7070
|-----|-------| -------- | ---- | ------|------------|---|-----|
7171
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/#/description)| [Python [Yu]](./tree/Yu/110_isBalanced.py) | _O(N)_| _O(h)_ | Easy | ||
7272
|112|[Path Sum](https://leetcode.com/problems/path-sum/#/description)| [Python [Yu]](./tree/Yu/112_hasPathSum.py) | _O(N)_| _O(h)_ | Easy | |[中文讲解](https://www.youtube.com/watch?v=LgtcGjIuE18&feature=youtu.be)|
73-
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/#/description)| [Python [Yu]](./tree/Yu/404_sum_of_Left_Leaves.py) | _O(N)_| _O(h)_ | Easy | ||
73+
7474

7575

7676

@@ -79,7 +79,7 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
7979
| # | Title | Solution | Time | Space | Difficulty |Tag| Note|
8080
|-----|-------| -------- | ---- | ------|------------|---|-----|
8181
|100|[Same Tree](https://leetcode.com/problems/same-tree/)| [Python [Yu]](./tree/Yu/100_isSameTree.py) <br> [Python [Sheng]](./tree/YeSheng/100.Same_Tree.py) | _O(N)_| _O(1)_ | Easy ||[EAFP vs. LBYL](https://github.com/ictar/python-doc/blob/master/Others/%E5%AE%9E%E7%94%A8Python%EF%BC%9AEAFP%20VS.%20LBYL.md)|
82-
|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/#/description)| [Python [Yu]](./tree/Yu/101_symmetricTree.py) <br> [Python [Sheng]](./tree/YeSheng/101.Symmetric_Tree.py) | _O(N)_| _O(1)_ | Easy ||[Editorial Solution](https://leetcode.com/articles/symmetric-tree/)|
82+
8383
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/#/description)| [Python [Yu]](https://github.com/yuzhoujr/LeetCode/blob/master/tree/Yu/111_minDepth.py) <br> [Python [Sheng]](https://github.com/yuzhoujr/LeetCode/blob/master/tree/YeSheng/111.Minimum_Depth_of_Binary_Tree.py) | _O(N)_| _O(1)_ | Easy |||
8484
|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| [Python](./tree/Yu/104_maxDepth.py) | _O(N)_| _O(1)_ | Easy | ||
8585
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/#/description)| [Python](./tree/Yu/235_lca_bst.py) | _O(N)_| _O(1)_ | Easy | ||
@@ -93,7 +93,13 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
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)|
9595
|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)|
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 | ||
97+
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/#/description)| [Python [Yu]](./tree/Yu/404_sum_of_Left_Leaves.py) | _O(N)_| _O(1)_ | Easy |
98+
|[公瑾讲解](https://youtu.be/pH-KxPcBF_4)|
99+
|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/#/description)| [Python [Yu]](./tree/Yu/101_symmetricTree.py)| _O(N)_| _O(1)_ | Easy ||[公瑾讲解](https://youtu.be/3Gl4F29LVpQ)|
100+
|270|[Closest Binary Search Tree Value](https://leetcode.com/problems/closest-binary-search-tree-value/#/description)| [Python [Yu]](./tree/Yu/270)| _O(N)_| _O(1)_ | Easy ||[公瑾讲解](https://youtu.be/RwvQyKJxHZk)|
101+
102+
97103

98104

99105
|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)|

tree/Yu/270.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Author: Yu Zhou
5+
# 270. Closest Binary Search Tree Value
6+
7+
# ****************
8+
# Descrption:
9+
# ****************
10+
11+
class Solution(object):
12+
def closestValue(self, root, target):
13+
14+
#Edge Case:
15+
if not root:
16+
return 0
17+
18+
self.res = root.val
19+
#Process
20+
def dfs(root, target):
21+
if not root:
22+
return 0
23+
if abs(target - self.res) > abs(target - root.val):
24+
self.res = root.val
25+
if target > root.val:
26+
dfs(root.right, target)
27+
else:
28+
dfs(root.left, target)
29+
30+
# Recursion
31+
dfs(root, target)
32+
return self.res

tree/Yu/404_sum_of_Left_Leaves.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ def sumOfLeftLeaves(self, root):
44
:type root: TreeNode
55
:rtype: int
66
"""
7+
# Edge
78
if not root:
89
return 0
10+
# Process
11+
self.count = 0
12+
def dfs(root):
13+
if not root:
14+
return 0
15+
if root.left and not root.left.left and not root.left.right:
16+
self.count += root.left.val
17+
# Recursion
18+
dfs(root.left)
19+
dfs(root.right)
920

10-
if root.left and root.left.left == None and root.left.right == None:
11-
return root.left.val + self.sumOfLeftLeaves(root.right)
12-
13-
left = self.sumOfLeftLeaves(root.left)
14-
right = self.sumOfLeftLeaves(root.right)
15-
16-
return left + right
21+
dfs(root)
22+
return self.count

0 commit comments

Comments
 (0)