Skip to content

Commit a20b61a

Browse files
committed
no message
1 parent 7e4c13b commit a20b61a

4 files changed

+130
-25
lines changed

problems/convert-sorted-array-to-binary-search-tree.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ def helper(i, j):
4949
if rightLength>0: node.right = helper(m+1, j)
5050
return node
5151

52-
return helper(0, len(nums))
52+
return helper(0, len(nums))
53+
54+
55+
56+
57+
class Solution(object):
58+
def sortedArrayToBST(self, nums):
59+
def helper(s, e):
60+
if s>e: return None
61+
m = (s+e)/2
62+
node = TreeNode(nums[m])
63+
node.left = helper(s, m-1)
64+
node.right = helper(m+1, e)
65+
return node
66+
return helper(0, len(nums)-1)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
self.dirs is a nested hashmap to store the file structure.
3+
self.files is a hashmap, mainly used to determine if the path is a "file", also used to store the content of the file.
4+
5+
Time:
6+
ls O(K)
7+
mkdir O(K)
8+
addContentToFile O(K)
9+
readContentFromFile O(1)
10+
K is the path count, for example /a/b/c/d, K=4.
11+
12+
Space:
13+
O(N), N is the dir counts.
14+
"""
15+
class FileSystem(object):
16+
17+
def __init__(self):
18+
self.files = {}
19+
self.dirs = {}
20+
21+
def ls(self, path):
22+
if path in self.files:
23+
return [path.split('/')[-1]]
24+
else:
25+
if path=='/': return sorted(self.dirs.keys())
26+
curr = self.dirs
27+
for d in path.split('/')[1:]:
28+
curr = curr[d]
29+
return sorted(curr.keys())
30+
31+
def mkdir(self, path):
32+
curr = self.dirs
33+
for d in path.split('/')[1:]:
34+
if d not in curr:
35+
curr[d] = {}
36+
curr = curr[d]
37+
38+
def addContentToFile(self, filePath, content):
39+
if filePath not in self.files:
40+
self.mkdir(filePath)
41+
self.files[filePath] = content
42+
else:
43+
self.files[filePath] += content
44+
45+
def readContentFromFile(self, filePath):
46+
return self.files[filePath]

problems/find-mode-in-binary-search-tree.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
Time: O(N). For we traverse every node recursively.
3+
Space: O(N). Becuase we store all element's val and count in `counter`.
4+
Note that, we do not use the feature of BST.
5+
"""
16
from collections import Counter
27
class Solution(object):
38
def findMode(self, root):
@@ -14,11 +19,21 @@ def count(node):
1419
return [val for val, count in counter.items() if count==max_count]
1520

1621
"""
17-
Time: O(N). For we traverse every node recursively.
18-
Space: O(N). Becuase we store all element's val and count in `counter`.
19-
Note that, we do not use the feature of BST.
20-
"""
22+
To use the feature of BST, we are going to inorder traverse the BST.
23+
So it will be like we are iterating a sorted array.
24+
25+
[1]
26+
While iterating, we can put only the element count that is greater or equal than `max_count` to `ans`.
27+
If we encounter a new element with larger `curr_count`, we reset the `ans`.
2128
29+
[0]
30+
With the help of `prev_val` we can know that `curr_node` is the same to the previous or not.
31+
If not, its a new element, we need to reset the `curr_count`.
32+
33+
Time: O(N). Space: O(LogN)
34+
35+
For better understanding, below is a template for inorder traverse.
36+
"""
2237
class Solution(object):
2338
def findMode(self, root):
2439
if not root: return []
@@ -54,23 +69,6 @@ def findMode(self, root):
5469

5570
return ans
5671

57-
"""
58-
To use the feature of BST, we are going to inorder traverse the BST.
59-
So it will be like we are iterating a sorted array.
60-
61-
[1]
62-
While iterating, we can put only the element count that is greater or equal than `max_count` to `ans`.
63-
If we encounter a new element with larger `curr_count`, we reset the `ans`.
64-
65-
[0]
66-
With the help of `prev_val` we can know that `curr_node` is the same to the previous or not.
67-
If not, its a new element, we need to reset the `curr_count`.
68-
69-
Time: O(N). Space: O(LogN)
70-
71-
For better understanding, below is a template for inorder traverse.
72-
"""
73-
7472
#inorder traversal of BST
7573
def inorder_traverse(root):
7674
curr = root
@@ -85,3 +83,28 @@ def inorder_traverse(root):
8583
print curr.val
8684

8785
curr = curr.right
86+
87+
88+
89+
90+
"""
91+
Time: O(N)
92+
Space: O(N)
93+
"""
94+
class Solution(object):
95+
def findMode(self, root):
96+
def helper(node):
97+
if not node: return
98+
counter[node.val] += 1
99+
counter['maxCount'] = max(counter['maxCount'], counter[node.val])
100+
helper(node.left)
101+
helper(node.right)
102+
103+
ans = []
104+
counter = collections.Counter()
105+
helper(root)
106+
for v in counter:
107+
if v!='maxCount' and counter[v]==counter['maxCount']:
108+
ans.append(v)
109+
110+
return ans

problems/insert-into-a-binary-search-tree.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Time complexity: O(LogN)
3+
Space complexity: O(1)
4+
"""
15
class Solution(object):
26
def insertIntoBST(self, root, val):
37
if not root: return TreeNode(val)
@@ -19,8 +23,26 @@ def insertIntoBST(self, root, val):
1923

2024
return None
2125

22-
2326
"""
2427
Time complexity: O(LogN)
25-
Space complexity: O(1)
26-
"""
28+
Space complexity: O(LogN)
29+
"""
30+
class Solution(object):
31+
def insertIntoBST(self, root, val):
32+
def helper(node, val):
33+
if not node: return
34+
if val<node.val:
35+
if node.left:
36+
helper(node.left, val)
37+
else:
38+
node.left = TreeNode(val)
39+
else:
40+
if node.right:
41+
helper(node.right, val)
42+
else:
43+
node.right = TreeNode(val)
44+
45+
if not root: return TreeNode(val)
46+
helper(root, val)
47+
return root
48+

0 commit comments

Comments
 (0)