Skip to content

Commit d26fdf3

Browse files
solves convert sorted array to bst in python
1 parent 283e09e commit d26fdf3

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/SymmetricTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/symmetric_tree.py) |
3333
| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MaximumDepthOfBinaryTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/maximum_depth_of_binary_tree.py) |
3434
| 107 | [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/BinaryTreeLevelOrderTraversalII.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/binary_tree_level_order_traversal_ii.py) |
35-
| 108 | [Convert Sorted Array To Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ConvertSortedArrayToBinarySearchTree.java) |
35+
| 108 | [Convert Sorted Array To Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ConvertSortedArrayToBinarySearchTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/converted_sorted_array_to_binary_search_tree.py) |
3636
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/BalancedBinaryTree.java) |
3737
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MinimumDepthOfBinaryTree.java) |
3838
| 112 | [Path Sum](https://leetcode.com/problems/path-sum) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PathSum.java) |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for a binary tree node.
2+
from typing import List, Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
def __repr__(self):
12+
return f'Tree(val={self.val})'
13+
14+
15+
class Solution:
16+
def sorted_array_to_bst(self, array: List[int], start: int, end: int) -> Optional[TreeNode]:
17+
if end == start:
18+
return None
19+
20+
middle = (start + end) // 2
21+
root = TreeNode(val=array[middle])
22+
root.left = self.sorted_array_to_bst(array, start, middle)
23+
root.right = self.sorted_array_to_bst(array, middle + 1, end)
24+
return root
25+
26+
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
27+
return self.sorted_array_to_bst(nums, 0, len(nums))

0 commit comments

Comments
 (0)