Sponsored
Sponsored
This approach uses a hash map to create nodes and establish their parent-child relationships. The root node is determined by finding nodes that haven't been a child in any relationship.
Time Complexity: O(N), where N is the number of descriptions.
Space Complexity: O(V), where V is the number of unique nodes.
1class TreeNode {
2 constructor(val, left = null, right = null) {
3 this.val = val;
4 this.left = left;
5 this.right = right;
6 }
7}
8
9function createBinaryTree(descriptions) {
10 const nodeMap = new Map();
11 const children = new Set();
12
13 for (const [parent, child, isLeft] of descriptions) {
14 if (!nodeMap.has(parent)) {
15 nodeMap.set(parent, new TreeNode(parent));
16 }
17 if (!nodeMap.has(child)) {
18 nodeMap.set(child, new TreeNode(child));
19 }
20
21 if (isLeft === 1) {
22 nodeMap.get(parent).left = nodeMap.get(child);
23 } else {
24 nodeMap.get(parent).right = nodeMap.get(child);
25 }
26 children.add(child);
27 }
28
29 for (const [key, node] of nodeMap) {
30 if (!children.has(key)) {
31 return node;
32 }
33 }
34 return null;
35}
36
The JavaScript solution uses a Map to keep track of each TreeNode instance along with their values. A Set identifies nodes that serve as children. The tree is constructed through relationship mapping, and the root node is determined by checking which node hasn't been listed as a child.
This approach uses a two-pass algorithm: the first pass creates all nodes independently, and the second pass links them based on the descriptive relationships. This avoids simultaneous creation and linking, providing a clearer separation of concerns between node creation and linkage.
Time Complexity: O(N), where N is the number of descriptions.
Space Complexity: O(V), where V is the number of unique nodes.
using System.Collections.Generic;
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
public class Solution {
public TreeNode CreateBinaryTree(int[][] descriptions) {
Dictionary<int, TreeNode> nodeMap = new Dictionary<int, TreeNode>();
HashSet<int> children = new HashSet<int>();
// First pass: Create all nodes
foreach (var desc in descriptions) {
int parent = desc[0], child = desc[1];
if (!nodeMap.ContainsKey(parent))
nodeMap[parent] = new TreeNode(parent);
if (!nodeMap.ContainsKey(child))
nodeMap[child] = new TreeNode(child);
children.Add(child);
}
// Second pass: Link nodes
foreach (var desc in descriptions) {
int parent = desc[0], child = desc[1], isLeft = desc[2];
if (isLeft == 1) {
nodeMap[parent].left = nodeMap[child];
} else {
nodeMap[parent].right = nodeMap[child];
}
}
foreach (var key in nodeMap.Keys) {
if (!children.Contains(key)) {
return nodeMap[key];
}
}
return null;
}
}
The C# implementation employs a similar two-pass strategy. It maps each TreeNode to its value and marks children, using a second loop to build left and right mappings per description.