-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathno_sibling_tree.py
More file actions
33 lines (25 loc) · 792 Bytes
/
no_sibling_tree.py
File metadata and controls
33 lines (25 loc) · 792 Bytes
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
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def printSingleNode(root, hasSibling):
# hasSibling will check if root has both children
if root is None:
return
else:
# if root has one child, print that child data
if not hasSibling:
print("%d" % root.data)
printSingleNode(root.left, root.right is not None)
printSingleNode(root.right, root.left is not None)
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)