forked from adnanaziz/EPIJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst_from_sorted_array.py
35 lines (26 loc) · 1.12 KB
/
bst_from_sorted_array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import functools
from bst_node import BstNode
from test_framework import generic_test
from test_framework.binary_tree_utils import (binary_tree_height,
generate_inorder)
from test_framework.test_failure import TestFailure
from test_framework.test_utils import enable_executor_hook
def build_min_height_bst_from_sorted_array(A):
if len(A) == 0:
return None
n = len(A)
return BstNode(A[n//2],
build_min_height_bst_from_sorted_array(A[:n//2]),
build_min_height_bst_from_sorted_array(A[n//2+1:]))
@enable_executor_hook
def build_min_height_bst_from_sorted_array_wrapper(executor, A):
result = executor.run(
functools.partial(build_min_height_bst_from_sorted_array, A))
if generate_inorder(result) != A:
raise TestFailure("Result binary tree mismatches input array")
return binary_tree_height(result)
if __name__ == '__main__':
exit(
generic_test.generic_test_main(
"bst_from_sorted_array.py", 'bst_from_sorted_array.tsv',
build_min_height_bst_from_sorted_array_wrapper))