# program t print cousing in binary tree
# class Node
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# function for checking is sibling or not..
def checkSibling(root, a, b):
if root is None:
return 0
return ((root.left == a and root.right == b) or
(root.left == b and root.right == a) or
checkSibling(root.left, a, b) or
checkSibling(root.right, a, b))
def level(root, ptr, lev):
if root is None:
return 0
if root == ptr:
return lev
l = level(root.left, ptr, lev + 1)
if l != 0:
return l
return level(root.right, ptr, lev + 1)
def checkcousin(root, a, b):
if ((level(root, a, 1) == level(root, b, 1)) and
not (checkSibling(root, a, b))):
return 1
else:
return 0
root = Node(10)
root.left = Node(11)
root.right = Node(12)
root.left.left = Node(13)
root.left.right = Node(14)
root.left.right.right = Node(15)
root.right.left = Node(16)
root.right.right = Node(17)
root.right.left.right = Node(18)
root.left.left.left = Node(15)
node1 = root.left.right
node2 = root.right.right
node3= root.left.left
node4 = root.right.left
# check node node1 and node 2 is cousin or not.
print("Yes" if checkcousin(root, node1, node2) == 1 else "No")
# check node node3 and node 4 is cousin or not.
print("Yes" if checkcousin(root, node3, node4) == 1 else "No")