-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathIterative In-order Traversal.py
49 lines (46 loc) · 1.83 KB
/
Iterative In-order Traversal.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
"""
Iterative In-order Traversal ☆
Write a function that takes in a Binary Tree (where nodes have an additional pointer to their parent node) and traverses
it iteratively using the in-order tree-traversal technique; the traversal should specifically not use recursion. As the
tree is being traversed, a callback function passed in as anargument to the main function should be called on each node
(i.e., callback(currentNode) ).
Each BinaryTree node has an integer value , a parent node, 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 6 7
\
9
callback = someCallback
Sample Output
// The input callback will have been called in the following order:
callback(4)
callback(9)
callback(2)
callback(1)
callback(6)
callback(3)
callback(7)
"""
# SOLUTION 1
# O(n) time | 0(1) space
def iterativeInOrderTraversal(tree, callback):
previousNode = None
currentNode = tree
while currentNode is not None:
if previousNode is None or previousNode == currentNode.parent:
if currentNode.left is not None:
nextNode = currentNode.left
else:
callback(currentNode)
nextNode = currentNode.right if currentNode.right is not None else currentNode.parent
elif previousNode == currentNode. left:
callback(currentNode)
nextNode = currentNode.right if currentNode.right is not None else currentNode.parent
else:
nextNode = currentNode.parent
previousNode = currentNode
currentNode = nextNode