-
Notifications
You must be signed in to change notification settings - Fork 93
/
CheckCousinsInBinaryTree.py
77 lines (72 loc) · 1.98 KB
/
CheckCousinsInBinaryTree.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
75
76
77
## Read input as specified in the question.
## Print output as specified in the question.
import queue
class BinaryTreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def isSibling(root,p,q):
if root is None:
return False
if root.left is not None and root.right is not None:
if (root.left.data==p and root.right.data==q):
return True
if (root.left.data==q and root.right.data==p):
return True
ans1=isSibling(root.left,p,q)
ans2=isSibling(root.right,p,q)
return ans1 or ans2
def depth(root,data):
if root is None :
return -1
if root.data==data:
return 0
leftDepth=depth(root.left,data)
if leftDepth!=-1:
return leftDepth+1
rightDepth=depth(root.right,data)
if rightDepth!=-1:
return rightDepth+1
return -1
def checkCousins(root,p,q):
if depth(root,p)==depth(root,q) and isSibling(root,p,q)is False:
return True
else :
return False
#Implement Your Code Here
pass
def buildLevelTree(levelorder):
index = 0
length = len(levelorder)
if length<=0 or levelorder[0]==-1:
return None
root = BinaryTreeNode(levelorder[index])
index += 1
q = queue.Queue()
q.put(root)
while not q.empty():
currentNode = q.get()
leftChild = levelorder[index]
index += 1
if leftChild != -1:
leftNode = BinaryTreeNode(leftChild)
currentNode.left =leftNode
q.put(leftNode)
rightChild = levelorder[index]
index += 1
if rightChild != -1:
rightNode = BinaryTreeNode(rightChild)
currentNode.right =rightNode
q.put(rightNode)
return root
# Main
levelOrder = [int(i) for i in input().strip().split()]
root = buildLevelTree(levelOrder)
p = int(input())
q = int(input())
ans = checkCousins(root,p,q)
if ans is True:
print('true')
else:
print('false')