Skip to content

Commit 12f1e6b

Browse files
add 2196
1 parent 73736f8 commit 12f1e6b

File tree

3 files changed

+73
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+73
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
| 2206 | [Divide Array Into Equal Pairs](https://leetcode.com/problems/divide-array-into-equal-pairs/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2206.java) || Easy ||
108108
| 2201 | [Count Artifacts That Can Be Extracted](https://leetcode.com/problems/count-artifacts-that-can-be-extracted/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2201.java) || Medium ||
109109
| 2200 | [Find All K-Distant Indices in an Array](https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2200.java) || Easy ||
110+
| 2196 | [Create Binary Tree From Descriptions](https://leetcode.com/problems/create-binary-tree-from-descriptions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2196.java) || Medium | HashTable, Tree
110111
| 2194 | [Cells in a Range on an Excel Sheet](https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2194.java) || Easy ||
111112
| 2190 | [Most Frequent Number Following Key In an Array](https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2190.java) || Easy ||
112113
| 2186 | [Minimum Number of Steps to Make Two Strings Anagram II](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2186.java) || Medium ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
public class _2196 {
11+
public static class Solution1 {
12+
/**
13+
* My completely original solution.
14+
*/
15+
public TreeNode createBinaryTree(int[][] descriptions) {
16+
Map<Integer, TreeNode> map = new HashMap<>();
17+
Set<Integer> notRootVals = new HashSet<>();
18+
for (int[] des : descriptions) {
19+
notRootVals.add(des[1]);
20+
TreeNode node = map.getOrDefault(des[0], new TreeNode(des[0]));
21+
if (des[2] == 1) {
22+
node.left = map.getOrDefault(des[1], new TreeNode(des[1]));
23+
map.put(des[1], node.left);
24+
} else {
25+
node.right = map.getOrDefault(des[1], new TreeNode(des[1]));
26+
map.put(des[1], node.right);
27+
}
28+
map.put(des[0], node);
29+
}
30+
int rootVal = -1;
31+
for (int[] des : descriptions) {
32+
if (!notRootVals.contains(des[0])) {
33+
rootVal = des[0];
34+
break;
35+
}
36+
}
37+
return map.get(rootVal);
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions.thirdthousand._2196;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _2196Test {
14+
private static _2196.Solution1 solution1;
15+
16+
@BeforeEach
17+
public void setup() {
18+
solution1 = new _2196.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(50, 20, 80, 15, 17, 19));
24+
TreeUtils.printBinaryTree(expected);
25+
TreeNode actual = solution1.createBinaryTree(new int[][]{
26+
{20, 15, 1}, {20, 17, 0}, {50, 20, 1}, {50, 80, 0}, {80, 19, 1}
27+
});
28+
TreeUtils.printBinaryTree(actual);
29+
assertEquals(expected, actual);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)