-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q09_bst_sequences.py
57 lines (46 loc) · 1.65 KB
/
Q09_bst_sequences.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import unittest
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def bst_sequences(bst):
return bst_sequences_partial([], [bst])
def bst_sequences_partial(partial, subtrees):
if not len(subtrees):
return [partial]
sequences = []
for index, subtree in enumerate(subtrees):
next_partial = partial + [subtree.val]
next_subtrees = subtrees[:index] + subtrees[index + 1:]
if subtree.left:
next_subtrees.append(subtree.left)
if subtree.right:
next_subtrees.append(subtree.right)
sequences += bst_sequences_partial(next_partial, next_subtrees)
return sequences
class Test(unittest.TestCase):
def test_bst_sequences(self):
tree = Node(2, Node(1), Node(3))
expected = [[2, 1, 3], [2, 3, 1]]
self.assertEqual(expected, bst_sequences(tree))
tree = Node(3, Node(2, Node(1)), Node(8))
expected = [[3, 2, 8, 1], [3, 2, 1, 8], [3, 8, 2, 1]]
self.assertEqual(expected, bst_sequences(tree))
self.assertEqual(
bst_sequences(Node(1, Node(8, Node(10)), Node(9))),
[[1, 8, 9, 10], [1, 8, 10, 9], [1, 9, 8, 10]],
)
self.assertEqual(
bst_sequences(Node(1, Node(8, Node(10), Node(6)), Node(9))),
[
[1, 8, 9, 10, 6],
[1, 8, 9, 6, 10],
[1, 8, 10, 9, 6],
[1, 8, 10, 6, 9],
[1, 8, 6, 9, 10],
[1, 8, 6, 10, 9],
[1, 9, 8, 10, 6],
[1, 9, 8, 6, 10],
],
)