-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy path326.py
58 lines (44 loc) · 1.42 KB
/
326.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
58
"""
Problem:
A Cartesian tree with sequence S is a binary tree defined by the following two
properties:
It is heap-ordered, so that each parent value is strictly less than that of its
children. An in-order traversal of the tree produces nodes with values that correspond
exactly to S. For example, given the sequence [3, 2, 6, 1, 9], the resulting Cartesian
tree would be:
1
/ \
2 9
/ \
3 6
Given a sequence S, construct the corresponding Cartesian tree.
"""
from typing import List, Optional
from DataStructures.Tree import Node, BinaryTree
def generate_cartesian_tree_helper(
arr: List[int], last: Optional[Node] = None, root: Optional[Node] = None
) -> Node:
if not arr:
return root
# Cartesian tree generation
node = Node(arr[0])
if not last:
# root of the tree
return generate_cartesian_tree_helper(arr[1:], node, node)
if last.val > node.val:
# property of Cartesian tree
node.left = last
return generate_cartesian_tree_helper(arr[1:], node, node)
last.right = node
return generate_cartesian_tree_helper(arr[1:], last, last)
def generate_cartesian_tree(sequence: List[int]) -> BinaryTree:
tree = BinaryTree()
tree.root = generate_cartesian_tree_helper(sequence)
return tree
if __name__ == "__main__":
print(generate_cartesian_tree([3, 2, 6, 1, 9]))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""