Create Binary Tree
Node map + child set + root detection · LC #2196
Medium
Cases
init
nodes
attach
root
done

Descriptions [[parent, child, isLeft], ...]

1[20, 15, 1]
parent 20child 15LEFT
2[20, 17, 0]
parent 20child 17RIGHT
3[50, 20, 1]
parent 50child 20LEFT
4[50, 80, 0]
parent 50child 80RIGHT
5[80, 19, 1]
parent 80child 19LEFT

Node Map & Child Set

All Nodes
none yet
Child Set (cannot be root)
none yet

Tree Preview

Left child Right child
Root not yet determined — tree will appear once root is found.
Complexity
Time
O(n)
Space
O(n)

Single pass over descriptions to build node map + child set. Root found in O(n) scan. Total O(n) time, O(n) space for the map.

Speed850ms
Progress1/13
Step Log
initNOW

Start: empty node map and child set. We will scan each description once.

Live State
Nodes
0
Children
0
Edges
0
Steps
Current
1
Total
13
Left
12