mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 18:07:32 +01:00
32 lines
764 B
Java
32 lines
764 B
Java
|
class Solution {
|
||
|
public TreeNode createBinaryTree(int[][] descriptions) {
|
||
|
Map<Integer, TreeNode> nodes = new HashMap<>();
|
||
|
|
||
|
Set<Integer> hasParent = new HashSet<>();
|
||
|
for (int[] description : descriptions) {
|
||
|
int parent = description[0];
|
||
|
int value = description[1];
|
||
|
boolean isLeft = description[2] == 1;
|
||
|
|
||
|
var parentNode = nodes.computeIfAbsent(parent, k -> new TreeNode(k));
|
||
|
var node = nodes.computeIfAbsent(value, k -> new TreeNode(k));
|
||
|
|
||
|
if (isLeft) {
|
||
|
parentNode.left = node;
|
||
|
} else {
|
||
|
parentNode.right = node;
|
||
|
}
|
||
|
|
||
|
hasParent.add(value);
|
||
|
}
|
||
|
|
||
|
for (var node : nodes.values()) {
|
||
|
if (!hasParent.contains(node.val)) {
|
||
|
return node;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
}
|