-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_traversal.py
More file actions
51 lines (40 loc) · 1.09 KB
/
Copy pathtree_traversal.py
File metadata and controls
51 lines (40 loc) · 1.09 KB
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
import pprint
class Node:
def __init__(self, data, left, right):
self.data = data
self.left = left
self.right = right
def inorder(root):
if root is None:
return
inorder(root.left)
print(str(root.data), end=' ')
inorder(root.right)
def inorderIterative(root):
if root is None:
return
stack = []
while len(stack) != 0 or root is not None:
while True:
if root is None:
break
stack.append(root)
root = root.left
root = stack.pop()
print(root.data, end=' ')
root = root.right
def main():
print('hello world')
root = Node(1, None, None)
# root.left = Node(2, None, None)
root.right = Node(3, None, None)
# root.left.left = Node(4, None, None)
# root.left.right = Node(5, None, None)
root.right.left = Node(6, None, None)
root.right.right = Node(7, None, None)
print('inorder traversal:')
inorder(root)
print('\n\ninorder iterative traversal:')
inorderIterative(root)
if __name__ == '__main__':
main()