-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path380.py
More file actions
26 lines (22 loc) · 791 Bytes
/
380.py
File metadata and controls
26 lines (22 loc) · 791 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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool:
x_info = (0, 0)
y_info = (0, 0)
def dfs(node, parent, depth):
nonlocal x_info, y_info
if node is None:
return
if node.val == x:
x_info = (parent, depth)
elif node.val == y:
y_info = (parent, depth)
dfs(node.left, node.val, depth + 1)
dfs(node.right, node.val, depth + 1)
dfs(root, 0, 1)
return x_info[0] != y_info[0] and x_info[1] == y_info[1]