class Solution:
def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
d = {}
def dfs(node, k):
if node != None:
if node.left != None:
d[node.left.val] = [node, k+1]
dfs(node.left, k+1)
if node.right != None:
d[node.right.val] = [node, k+1]
dfs(node.right, k+1)
dfs(root, 0)
return x in d and y in d and (d[x][1] == d[y][1] and d[x][0] != d[y][0])