-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFlatten Binary Tree.py
91 lines (72 loc) · 2.94 KB
/
Flatten Binary Tree.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Flatten Binary Tree O ☆
Write a function that takes in a Binary Tree, flattens it, and returns its leftmost node.
A flattened Binary Tree is a structure that's nearly identical to a Doubly Linked List (except that nodes have left and
right pointers instead of prev and next pointers), where nodes follow the original tree's left-to-right order.
Note that if the input Binary Tree happens to be a valid Binary Search Tree, the nodes in the flattened tree will be sorted.
The flattening should be done in place, meaning that the original data structure should be mutated (no new structure
should be created).
Each BinaryTree node has an integer value , a left child node, and a right child node. Children nodes can
either be BinaryTree nodes themselves or None / null .
Sample Input
tree = 1
/ \
2 3
/ \ /
4 5 6
/ \
7 8
Sample Output
4 <-> 2 <-> 1 <-> 5 <-> 8 <-> 1 <-> 6 <-> 3 // the leftmost node with value 4
"""
# SOLUTION 1
# This is the class of the input root. Do not edit it.
class BinaryTree:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
# O(n) time | O(n) space - where n is the number of nodes in the Binary Tree
def flattenBinaryTree(root):
inOrderNodes = getNodesInOrder(root, [])
for i in range(0, len(inOrderNodes) - 1):
leftNode = inOrderNodes[i]
rightNode = inOrderNodes[i + 1]
leftNode.right = rightNode
rightNode.left = leftNode
return inOrderNodes[0]
def getNodesInOrder(tree, array):
if tree is not None:
getNodesInOrder(tree.left, array)
array.append(tree)
getNodesInOrder(tree.right, array)
return array
# SOLUTION 2
# This is the class of the input root. Do not edit it.
class BinaryTree:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
# O(n) time | 0(d) space - where n is the number of nodes in the Binary Tree
# and d is the depth (height) of the Binary Tree
def flattenBinaryTree(root):
leftMost, _ = flattenTree(root)
return leftMost
def flattenTree(node):
if node.left is None:
leftMost = node
else:
leftSubtreeLeftMost, leftSubtreeRightMost = flattenTree(node.left)
connectNodes(leftSubtreeRightMost, node)
leftMost = leftSubtreeLeftMost
if node.right is None:
rightMost = node
else:
rightSubtreeLeftMost, rightSubtreeRightMost = flattenTree(node.right)
connectNodes(node, rightSubtreeLeftMost)
rightMost = rightSubtreeRightMost
return [leftMost, rightMost]
def connectNodes(left, right):
left.right = right
right.left = left