-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPreorderTraversal.py
74 lines (63 loc) · 1.77 KB
/
PreorderTraversal.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
"""
@author Anirudh Sharma
Given a binary tree, write an iterative and recursive solution to traverse the tree using preorder traversal.
"""
def preorderTraversalRecursively(root):
# Base case
if root is None:
return
print(root.data, end=" ")
preorderTraversalRecursively(root.left)
preorderTraversalRecursively(root.right)
def preorderTraversalIteratively(root):
if root is None:
return None
# Stack to store the nodes of the tree
nodes = []
# Add root node to the stack
nodes.append(root)
# Loop until stack is empty
while nodes:
# Get current node
current = nodes.pop()
print(current.data, end=" ")
# Push right child if not null
if current.right:
nodes.append(current.right)
# Push left child if not null
if current.left:
nodes.append(current.left)
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
if __name__ == "__main__":
root = Node(1)
root.left = Node(3)
root.right = Node(2)
print("Recursive:")
preorderTraversalRecursively(root)
print("\nIterative:")
preorderTraversalIteratively(root)
print("\n")
root = Node(10)
root.left = Node(20)
root.right = Node(30)
root.left.left = Node(40)
root.left.right = Node(60)
print("Recursive:")
preorderTraversalRecursively(root)
print("\nIterative:")
preorderTraversalIteratively(root)
print("\n")
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Recursive:")
preorderTraversalRecursively(root)
print("\nIterative:")
preorderTraversalIteratively(root)
print("\n")