Skip to content

Commit b6eb88d

Browse files
committed
108
1 parent 38701d0 commit b6eb88d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,6 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
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 | ||
86+
87+
## 5/2 Tasks (Tree Easy)
88+
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/#/solutions)| [Python [Yu]](./tree/Yu/108.py) | _O(N)_| _O(N)_ | Easy | |[公瑾讲解](https://www.youtube.com/watch?v=lBrb4fXPcMM)|

tree/Yu/108.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+
6+
# ****************
7+
# 108. Convert Sorted Array to Binary Search Tree
8+
# Descrption:
9+
# Given an array where elements are sorted in ascending order,
10+
# convert it to a height balanced BST.
11+
# ****************
12+
13+
# 思路:
14+
# 基础递归
15+
# 每层的任务是,创建mid节点。
16+
17+
# ****************
18+
# Final Solution *
19+
# ****************
20+
class Solution(object):
21+
def sortedArrayToBST(self, nums):
22+
"""
23+
:type nums: List[int]
24+
:rtype: TreeNode
25+
"""
26+
#Edge:
27+
if not nums:
28+
return None
29+
30+
mid = len(nums) / 2
31+
32+
root = TreeNode(nums[mid])
33+
root.left = self.sortedArrayToBST(nums[:mid])
34+
root.right = self.sortedArrayToBST(nums[mid+1:])
35+
36+
return root
37+
38+
# ***************************************
39+
# The following code is an fail attempt *
40+
# ***************************************

0 commit comments

Comments
 (0)