-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcousins_in_binary_tree.py
More file actions
33 lines (30 loc) · 1.05 KB
/
Copy pathcousins_in_binary_tree.py
File metadata and controls
33 lines (30 loc) · 1.05 KB
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
from collections import deque
class Solution: # 40ms
def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
queue = deque([(root, 0, None)])
depth_x = None
depth_y = None
while queue:
node, depth, parent = queue.popleft()
if node.val == x:
depth_x = depth
parent_x = parent
elif node.val == y:
depth_y = depth
parent_y = parent
if depth_x and depth_y:
return (depth_x == depth_y and parent_x != parent_y)
if node.left:
queue.append((node.left, depth+1, node))
if node.right:
queue.append((node.right, depth+1, node))
if not depth_x or not depth_y:
return False
else:
return (depth_x == depth_y and parent_x != parent_y)