Skip to content

Commit 525f268

Browse files
committed
Cousins in Binary Tree
1 parent 28e3b98 commit 525f268

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

993-cousins-in-binary-tree.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/cousins-in-binary-tree/
3+
4+
In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.
5+
Two nodes of a binary tree are cousins if they have the same depth, but have different parents.
6+
We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.
7+
Return true if and only if the nodes corresponding to the values x and y are cousins.
8+
9+
Example 1:
10+
Input: root = [1,2,3,4], x = 4, y = 3
11+
Output: false
12+
13+
Example 2:
14+
Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
15+
Output: true
16+
17+
Example 3:
18+
Input: root = [1,2,3,null,4], x = 2, y = 3
19+
Output: false
20+
21+
Constraints:
22+
The number of nodes in the tree will be between 2 and 100.
23+
Each node has a unique integer value from 1 to 100.
24+
"""
25+
# Definition for a binary tree node.
26+
# class TreeNode:
27+
# def __init__(self, val=0, left=None, right=None):
28+
# self.val = val
29+
# self.left = left
30+
# self.right = right
31+
class Solution:
32+
def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
33+
34+
def helper(root, parent, level, values, parents):
35+
if len(parents) == 2:
36+
# print(parents)
37+
38+
return parents[0][0] != parents[1][0] and parents[0][1] == parents[1][1]
39+
if not root:
40+
return False
41+
42+
if root.val in values:
43+
parents.append((parent, level))
44+
45+
a = helper(root.left, root, level+1, values, parents)
46+
b = helper(root.right, root, level+1, values, parents)
47+
return a or b
48+
49+
return helper(root, None, 0, [x,y], [])
50+
51+
52+
class Solution1:
53+
def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
54+
parent = parent = None
55+
queue = [[root]]
56+
while queue:
57+
level = queue.pop(0)
58+
next_level = []
59+
while level:
60+
node = level.pop()
61+
if node.left:
62+
if node.left.val in [x,y]:
63+
if parent:
64+
return parent != node.val
65+
parent = node.val
66+
67+
next_level.append(node.left)
68+
69+
if node.right:
70+
if node.right.val in [x,y]:
71+
if parent:
72+
return parent != node.val
73+
parent = node.val
74+
75+
next_level.append(node.right)
76+
77+
if next_level:
78+
queue.append(next_level)
79+
80+
if parent:
81+
return False
82+
return False
83+
84+
85+
class Solution2:
86+
def isCousins(self, root, x, y):
87+
"""
88+
:type root: TreeNode
89+
:type x: int
90+
:type y: int
91+
:rtype: bool
92+
"""
93+
lookUp = {}
94+
def dfs(root,depth=0,parent=None):
95+
if not root:
96+
return None
97+
if root.val in (x,y): lookUp[root.val] = (depth,parent)
98+
dfs(root.left,depth + 1, root.val)
99+
dfs(root.right,depth + 1, root.val)
100+
dfs(root)
101+
return lookUp[x][0] == lookUp[y][0] and lookUp[x][1] != lookUp[y][1]

0 commit comments

Comments
 (0)