-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
cousins-in-binary-tree.py
43 lines (37 loc) · 1.15 KB
/
cousins-in-binary-tree.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
# Time: O(n)
# Space: O(h)
# 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):
"""
:type root: TreeNode
:type x: int
:type y: int
:rtype: bool
"""
def dfs(root, x, depth, parent):
if not root:
return False
if root.val == x:
return True
depth[0] += 1
prev_parent, parent[0] = parent[0], root
if dfs(root.left, x, depth, parent):
return True
parent[0] = root
if dfs(root.right, x, depth, parent):
return True
parent[0] = prev_parent
depth[0] -= 1
return False
depth_x, depth_y = [0], [0]
parent_x, parent_y = [None], [None]
return dfs(root, x, depth_x, parent_x) and \
dfs(root, y, depth_y, parent_y) and \
depth_x[0] == depth_y[0] and \
parent_x[0] != parent_y[0]