Skip to content

Commit 0b55353

Browse files
add a solution for 993
1 parent 6933963 commit 0b55353

File tree

2 files changed

+66
-31
lines changed

2 files changed

+66
-31
lines changed

src/main/java/com/fishercoder/solutions/_993.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5+
import java.util.HashMap;
56
import java.util.HashSet;
67
import java.util.LinkedList;
8+
import java.util.Map;
79
import java.util.Queue;
810
import java.util.Set;
911

@@ -45,4 +47,33 @@ private boolean checkQueue(Queue<TreeNode> queue, int x, int y) {
4547
return set.contains(x) && set.contains(y);
4648
}
4749
}
50+
51+
public static class Solution2 {
52+
public boolean isCousins(TreeNode root, int x, int y) {
53+
Queue<TreeNode> queue = new LinkedList<>();
54+
queue.offer(root);
55+
Map<Integer, Integer> childToParentMap = new HashMap<>();
56+
while (!queue.isEmpty()) {
57+
int size = queue.size();
58+
childToParentMap.clear();
59+
for (int i = 0; i < size; i++) {
60+
TreeNode curr = queue.poll();
61+
if (curr.left != null) {
62+
queue.offer(curr.left);
63+
childToParentMap.put(curr.left.val, curr.val);
64+
}
65+
if (curr.right != null) {
66+
queue.offer(curr.right);
67+
childToParentMap.put(curr.right.val, curr.val);
68+
}
69+
}
70+
if (childToParentMap.containsKey(x) && childToParentMap.containsKey(y) && childToParentMap.get(x) != childToParentMap.get(y)) {
71+
return true;
72+
} else if (childToParentMap.containsKey(x) || childToParentMap.containsKey(y)) {
73+
return false;
74+
}
75+
}
76+
return false;
77+
}
78+
}
4879
}

src/test/java/com/fishercoder/_993Test.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,46 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
5-
import com.fishercoder.solutions._703;
6-
import com.fishercoder.solutions._976;
75
import com.fishercoder.solutions._993;
8-
import java.util.Arrays;
96
import org.junit.BeforeClass;
107
import org.junit.Test;
118

9+
import java.util.Arrays;
10+
1211
import static junit.framework.Assert.assertEquals;
1312

1413
public class _993Test {
15-
private static _993.Solution1 solution1;
16-
private static TreeNode root;
17-
18-
@BeforeClass
19-
public static void setUp() {
20-
solution1 = new _993.Solution1();
21-
}
22-
23-
@Test
24-
public void test1() {
25-
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4));
26-
TreeUtils.printBinaryTree(root);
27-
assertEquals(false, solution1.isCousins(root, 4, 3));
28-
}
29-
30-
@Test
31-
public void test2() {
32-
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4, null, 5));
33-
TreeUtils.printBinaryTree(root);
34-
assertEquals(true, solution1.isCousins(root, 5, 4));
35-
}
36-
37-
@Test
38-
public void test3() {
39-
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4));
40-
TreeUtils.printBinaryTree(root);
41-
assertEquals(false, solution1.isCousins(root, 2, 3));
42-
}
14+
private static _993.Solution1 solution1;
15+
private static _993.Solution2 solution2;
16+
private static TreeNode root;
17+
18+
@BeforeClass
19+
public static void setUp() {
20+
solution1 = new _993.Solution1();
21+
solution2 = new _993.Solution2();
22+
}
23+
24+
@Test
25+
public void test1() {
26+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4));
27+
TreeUtils.printBinaryTree(root);
28+
assertEquals(false, solution1.isCousins(root, 4, 3));
29+
assertEquals(false, solution2.isCousins(root, 4, 3));
30+
}
31+
32+
@Test
33+
public void test2() {
34+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4, null, 5));
35+
TreeUtils.printBinaryTree(root);
36+
assertEquals(true, solution1.isCousins(root, 5, 4));
37+
assertEquals(true, solution2.isCousins(root, 5, 4));
38+
}
39+
40+
@Test
41+
public void test3() {
42+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4));
43+
TreeUtils.printBinaryTree(root);
44+
assertEquals(false, solution1.isCousins(root, 2, 3));
45+
assertEquals(false, solution2.isCousins(root, 2, 3));
46+
}
4347
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy