Create Binary Tree From Descriptions - Complete Solution Guide
Create Binary Tree From Descriptions is LeetCode problem 2196, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 2D integer array descriptions where descriptions[i] = [parent i , child i , isLeft i ] indicates that parent i is the parent of child i in a binary tree of unique values. Furthermore, If isLeft i == 1 , then child i is the left child of parent i . If isLeft i == 0 , then child i is the right child of parent i . 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: descripti
Detailed Explanation
The problem requires constructing a binary tree from a given array of descriptions. Each description provides information about a parent-child relationship, including whether the child is the left or right child of the parent. The input is a 2D integer array `descriptions`, where each row represents a parent-child relationship: `[parent_i, child_i, isLeft_i]`. The goal is to return the root node of the constructed binary tree. The tree contains unique values, and the provided descriptions guarantee a valid binary tree.
Solution Approach
The solution uses a hash map (`nodes_map`) to store `TreeNode` objects, keyed by their values. It also uses a set (`children_set`) to track all the child nodes. The algorithm iterates through the `descriptions` array. For each description, it creates `TreeNode` objects for the parent and child (if they don't already exist in the `nodes_map`), adds the child to `children_set`, and establishes the parent-child relationship (left or right) based on `isLeft`. After processing all descriptions, the algorithm iterates through the descriptions again to find the root node. It identifies the root as the node whose value is not present in the `children_set`. Finally, it returns the `TreeNode` object corresponding to the root value from `nodes_map`.
Step-by-Step Algorithm
- Step 1: Initialize a hash map `nodes_map` to store the tree nodes (TreeNode objects) with the node values as keys. Initialize a set `children_set` to store the values of all child nodes.
- Step 2: Iterate through the `descriptions` array.
- Step 3: For each description `[parent_val, child_val, is_left]`, add `child_val` to the `children_set`.
- Step 4: If `parent_val` is not in `nodes_map`, create a new `TreeNode` with value `parent_val` and add it to `nodes_map`.
- Step 5: If `child_val` is not in `nodes_map`, create a new `TreeNode` with value `child_val` and add it to `nodes_map`.
- Step 6: Set the left or right child of the parent node in `nodes_map` to the child node, based on the value of `is_left` (1 for left, 0 for right).
- Step 7: After processing all descriptions, iterate through the `descriptions` array again.
- Step 8: For each description, check if the `parent_val` is present in the `children_set`. If not, it means `parent_val` is the root node's value.
- Step 9: Return the `TreeNode` object from `nodes_map` that corresponds to the root node's value.
Key Insights
- Insight 1: The core idea is to use a hash map (or dictionary) to store nodes and their corresponding TreeNode objects. This allows efficient access and manipulation of nodes based on their values.
- Insight 2: Identifying the root node is crucial. The root is the only node that is not a child of any other node. This can be determined by tracking all child nodes and finding the node that isn't in the set of children.
- Insight 3: The constraint that the tree is valid simplifies the process because we do not have to handle invalid inputs such as cycles or multiple roots.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Tree, Binary Tree.
Companies
Asked at: Clari, LinkedIn.