-
Notifications
You must be signed in to change notification settings - Fork 0
/
cousins_bt.py
67 lines (53 loc) · 1.86 KB
/
cousins_bt.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
from collections import deque
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def isCousins(self, root, x, y): #!SIMPLIFIED version of the below function
queue = deque()
queue.append((root, 0))
dist={}
while queue:
node,depth = queue.popleft()
if node.left:
dist[node.left.val] = (node.val, depth+1)
queue.append((node.left, depth+1))
if node.right:
dist[node.right.val] = (node.val, depth+1)
queue.append((node.right, depth+1))
if x not in dist or y not in dist:
return False
x_parent, x_depth = dist[x]
y_parent, y_depth = dist[y]
return x_depth == y_depth and x_parent != y_parent
def isCousins(self, root, x, y):
"""
:type root: TreeNode
:type x: int
:type y: int
:rtype: bool
"""
x_found, y_found = False, False
x_parent, y_parent = None, None
x_depth, y_depth = 0, 0
q = [(0,root,None)]
while q:
d, node, par = q.pop()
if node.val == x:
x_found = True
x_parent = par
x_depth = d
elif node.val == y:
y_found = True
y_parent = par
y_depth = d
if node.left:
q.append((d+1,node.left,node))
if node.right:
q.append((d+1,node.right,node))
if x_found and y_found:
break
return( (x_depth==y_depth) and (x_parent!=y_parent) and x_found and y_found)