Watch 10 video solutions for Create Binary Tree From Descriptions, a medium level problem involving Array, Hash Table, Tree. This walkthrough by NeetCodeIO has 13,270 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 2D integer array descriptions where descriptions[i] = [parenti, childi, isLefti] indicates that parenti is the parent of childi in a binary tree of unique values. Furthermore,
isLefti == 1, then childi is the left child of parenti.isLefti == 0, then childi is the right child of parenti.Construct the binary tree described by descriptions and return its root.
The test cases will be generated such that the binary tree is valid.
Example 1:
Input: descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]] Output: [50,20,80,15,17,19] Explanation: The root node is the node with value 50 since it has no parent. The resulting binary tree is shown in the diagram.
Example 2:
Input: descriptions = [[1,2,1],[2,3,0],[3,4,1]] Output: [1,2,null,null,3,4] Explanation: The root node is the node with value 1 since it has no parent. The resulting binary tree is shown in the diagram.
Constraints:
1 <= descriptions.length <= 104descriptions[i].length == 31 <= parenti, childi <= 1050 <= isLefti <= 1descriptions is valid.Problem Overview: You receive a list of descriptions where each entry contains [parent, child, isLeft]. The task is to construct the binary tree represented by these relationships and return the root node. Each pair tells you the parent value, the child value, and whether the child is the left or right node.
Approach 1: Hash Map to Track Parent-Child Relationships (O(n) time, O(n) space)
The main challenge is that nodes may appear in any order, so you cannot assume the parent node already exists when processing a description. Use a hash map that maps node values to actual TreeNode objects. Iterate through the descriptions array once, creating nodes if they do not already exist. For every entry, link the child node to the parent node based on the isLeft flag. At the same time, track all values that appear as children using a set. After processing all descriptions, the root is the node that never appeared as a child. This approach performs constant-time hash lookups for node creation and linking, making the entire process linear.
This method works well because it decouples node creation from tree structure. You simply ensure every value maps to exactly one node object, then connect them using the relationships provided. Hash-based lookup avoids repeated scans and ensures each description is processed once.
Approach 2: Linking Nodes Using Two-Pass Array (O(n) time, O(n) space)
Another strategy separates node creation and node linking into two passes. During the first pass, iterate through the descriptions and create node objects for every unique value you encounter. Store them in an indexed structure or hash map for quick access. In the second pass, iterate again and connect children to parents using the isLeft flag. Maintain a boolean array or set to mark nodes that appear as children. After linking, the root is the node that was never marked as a child.
This approach keeps the logic straightforward by avoiding conditional node creation while linking. The first pass guarantees all nodes exist, and the second pass strictly focuses on building the edges. The complexity remains linear because each description is processed a constant number of times.
Both approaches rely heavily on efficient lookups using structures from hash tables and sequential processing of the array input. The final structure produced is a standard binary tree.
Recommended for interviews: The hash map approach is the expected solution. It constructs nodes and links them in a single pass while tracking the root using a child set. Showing the two-pass method demonstrates clarity in separating responsibilities, but the single-pass hash map version highlights stronger algorithmic efficiency and cleaner reasoning.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Hash Map to Track Parent-Child Relationships | O(n) | O(n) | Best general solution. Builds nodes and connects them in one pass with constant-time lookups. |
| Two-Pass Node Creation and Linking | O(n) | O(n) | Useful when you want clearer separation between node creation and tree construction. |