Skip to content

Commit bdfebfb

Browse files
committed
Runtime: 2 ms (Top 20.27%) | Memory: 42 MB (Top 40.06%)
1 parent bf31fd3 commit bdfebfb

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
// Runtime: 2 ms (Top 20.27%) | Memory: 42 MB (Top 40.06%)
12
class Solution {
23
public boolean isCousins(TreeNode root, int x, int y) {
34
Set<TreeNode> parentSet = new HashSet<>();
45
Queue<TreeNode> q = new LinkedList<>();
56
q.add(root);
6-
7+
78
while (!q.isEmpty()) {
89
int len = q.size();
9-
10+
1011
for (int i = 0; i < len; i++) {
1112
TreeNode parent = q.remove();
12-
13+
1314
for (TreeNode child : new TreeNode[]{parent.left, parent.right}) {
1415
if(child != null) {
1516
q.add(child);
@@ -19,8 +20,8 @@ public boolean isCousins(TreeNode root, int x, int y) {
1920
}
2021
}
2122
if (parentSet.size() > 0)
22-
return parentSet.size() == 2; //if same parent -> set size wil be 1
23+
return parentSet.size() == 2; //if same parent -> set size wil be 1
2324
}
2425
return false;
2526
}
26-
}
27+
}

0 commit comments

Comments
 (0)