- Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathno_sibling_tree.py
33 lines (25 loc) · 792 Bytes
/
no_sibling_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
# Problem: Print the nodes of a binary tree
# which do not have a sibling
classNode:
def__init__(self, data):
self.data=data
self.left=None
self.right=None
defprintSingleNode(root, hasSibling):
# hasSibling will check if root has both children
ifrootisNone:
return
else:
# if root has one child, print that child data
ifnothasSibling:
print("%d"%root.data)
printSingleNode(root.left, root.rightisnotNone)
printSingleNode(root.right, root.leftisnotNone)
root=Node(1)
root.left=Node(2)
root.right=Node(3)
root.left.left=Node(4)
root.right.left=Node(5)
root.right.left.left=Node(6)
print("Level order traversal of binary tree is -")
printSingleNode(root, True)