Skip to content

Commit 2e211b0

Browse files
committed
[bhyun-kim, 김병현] Week 8 Solutions
1 parent f0f8b4d commit 2e211b0

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
105. Construct Binary Tree from Preorder and Inorder Traversal
3+
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
4+
"""
5+
6+
from typing import List, Optional
7+
8+
# Definition for a binary tree node.
9+
class TreeNode:
10+
def __init__(self, val=0, left=None, right=None):
11+
self.val = val
12+
self.left = left
13+
self.right = right
14+
15+
"""
16+
Solution
17+
To solve this problem, we can use a recursive approach.
18+
We can use a helper function that takes the left and right index of the inorder array.
19+
The helper function will create a root node with the value of the current preorder index.
20+
Then, it will recursively call itself with the left and right index of the left and right subtree.
21+
22+
- Create a dictionary that maps the value of the inorder array to its index.
23+
- Create a variable to keep track of the current preorder index.
24+
- Create a helper function that takes the left and right index of the inorder array.
25+
- In the helper function, if the left index is greater than the right index, return None.
26+
- Create a root node with the value of the current preorder index.
27+
- Increment the preorder index.
28+
- Recursively call the helper function with the left and right index of the left and right subtree.
29+
- Return the root node.
30+
31+
Time Complexity : O(n)
32+
- The helper function is called n times.
33+
- The dictionary lookup is O(1).
34+
35+
Space Complexity : O(n)
36+
- The dictionary has n elements.
37+
- The recursive call stack has n elements.
38+
"""
39+
40+
class Solution:
41+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
42+
inorder_index_map = {val: idx for idx, val in enumerate(inorder)}
43+
44+
self.preorder_index = 0
45+
46+
def array_to_tree(left, right):
47+
if left > right:
48+
return None
49+
50+
root_value = preorder[self.preorder_index]
51+
self.preorder_index += 1
52+
53+
root = TreeNode(root_value)
54+
55+
root.left = array_to_tree(left, inorder_index_map[root_value] - 1)
56+
root.right = array_to_tree(inorder_index_map[root_value] + 1, right)
57+
58+
return root
59+
60+
return array_to_tree(0, len(inorder) - 1)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
208. Implement Trie (Prefix Tree)
3+
https://leetcode.com/problems/implement-trie-prefix-tree/
4+
5+
Solution:
6+
- Initialize the Trie class with an empty list of words.
7+
- Insert a word by appending it to the list of words.
8+
- Search for a word by checking if it is in the list of words.
9+
- Check if a prefix exists by iterating through the list of words and checking if any word starts with the prefix.
10+
11+
Tiem complexity:
12+
- Insert: O(1)
13+
- Appending to a list is O(1)
14+
- Search: O(n)
15+
- Searching for an element in a list is O(n)
16+
- StartsWith: O(n)
17+
- Iterating through a list is O(n)
18+
19+
Space complexity: O(n)
20+
- The list of words may have all the words in the trie.
21+
"""
22+
23+
class Trie:
24+
25+
def __init__(self):
26+
self.words = []
27+
28+
def insert(self, word: str) -> None:
29+
self.words.append(word)
30+
31+
def search(self, word: str) -> bool:
32+
return word in self.words
33+
34+
def startsWith(self, prefix: str) -> bool:
35+
for word in self.words:
36+
if word.startswith(prefix):
37+
return True
38+
39+
return False
40+
41+
42+
43+
# Your Trie object will be instantiated and called as such:
44+
# obj = Trie()
45+
# obj.insert(word)
46+
# param_2 = obj.search(word)
47+
# param_3 = obj.startsWith(prefix)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
230. Kth Smallest Element in a BST
3+
https://leetcode.com/problems/kth-smallest-element-in-a-bst/
4+
5+
Solution:
6+
To solve this problem, we can use an in-order traversal of the binary search tree.
7+
We can create a helper function that performs an in-order traversal of the tree and returns a list of the elements in sorted order.
8+
Then, we can return the k-th element from the list.
9+
10+
Time complexity: O(n)
11+
- The in-order traversal visits each node once.
12+
- The list concatenation is O(n).
13+
14+
Space complexity: O(n)
15+
- The list of elements has n elements.
16+
"""
17+
18+
19+
from typing import Optional
20+
21+
# Definition for a binary tree node.
22+
class TreeNode:
23+
def __init__(self, val=0, left=None, right=None):
24+
self.val = val
25+
self.left = left
26+
self.right = right
27+
28+
class Solution:
29+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
30+
return self.inOrderSearch(root)[k-1]
31+
32+
def inOrderSearch(self, root):
33+
output = []
34+
if root:
35+
output += self.inOrderSearch(root.left)
36+
output += [root.val]
37+
output += self.inOrderSearch(root.right)
38+
return output

0 commit comments

Comments
 (0)