LeetCode 2196: Create Binary Tree From Descriptions – Java HashMap & Tree Construction Solution

Learn how to solve LeetCode 2196 Create Binary Tree From Descriptions using Java HashMap and HashSet. Includes intuition, step-by-step explanation, dry run, optimized approach, and complexity analysis.

Krishna Shrivastava
1 views
LinkedInGithubX
0
0
LeetCode 2196: Create Binary Tree From Descriptions – Java HashMap & Tree Construction Solution
Listen to articleAudio version
Ad
Advertisement

Introduction

Binary tree construction problems are extremely common in coding interviews because they test multiple concepts together:

  1. Tree data structures
  2. HashMap usage
  3. Parent-child relationships
  4. Graph-style thinking
  5. Root identification

LeetCode 2196 is an excellent medium-level problem that focuses on constructing a binary tree from parent-child descriptions efficiently.

In this article, we will deeply understand:

  1. How the tree is formed
  2. How to identify the root node
  3. Why HashMap is useful here
  4. Step-by-step dry run
  5. Time and space complexity
  6. Complete optimized Java solution

Problem Statement

You are given a 2D array:

descriptions[i] = [parent, child, isLeft]

Where:

  1. parent is the parent node
  2. child is the child node
  3. isLeft == 1 means left child
  4. isLeft == 0 means right child

Your task is to:

  1. Construct the binary tree
  2. Return the root node

Here is the Link to try it now -: Create Binary Tree From Descriptions

Example

Input

descriptions = [
[20,15,1],
[20,17,0],
[50,20,1],
[50,80,0],
[80,19,1]
]

Output

[50,20,80,15,17,19]

Visual Representation

50
/ \
20 80
/ \ /
15 17 19

Key Observation

Every node except the root appears as a child exactly once.

This means:

  1. Root node never appears in child positions
  2. If we store all child nodes,
  3. The node not present in the child set becomes the root

This is the core trick of the problem.

Approach Overview

We solve the problem in 3 steps:

Step 1: Find the Root Node

Store every child node in a HashSet.

Then iterate through descriptions again:

  1. The parent that never appears as a child is the root.

Step 2: Create Tree Nodes

Use a HashMap:

value → TreeNode

This prevents duplicate node creation.

Step 3: Connect Parent and Child

Based on:

isLeft

attach nodes accordingly.

Optimized Java Solution

class Solution {

public TreeNode createBinaryTree(int[][] descriptions) {

HashSet<Integer> childSet = new HashSet<>();

// Store all child nodes
for (int[] arr : descriptions) {
childSet.add(arr[1]);
}

// Find root node
int rootValue = -1;

for (int[] arr : descriptions) {

if (!childSet.contains(arr[0])) {
rootValue = arr[0];
}
}

// Map value -> TreeNode
HashMap<Integer, TreeNode> map = new HashMap<>();

for (int i = 0; i < descriptions.length; i++) {

int parent = descriptions[i][0];
int child = descriptions[i][1];
int isLeft = descriptions[i][2];

// Create parent node if absent
if (!map.containsKey(parent)) {
map.put(parent, new TreeNode(parent));
}

// Create child node if absent
if (!map.containsKey(child)) {
map.put(child, new TreeNode(child));
}

// Connect nodes
if (isLeft == 1) {

map.get(parent).left = map.get(child);

} else {

map.get(parent).right = map.get(child);
}
}

return map.get(rootValue);
}
}

Step-by-Step Explanation

Step 1: Store All Child Nodes

childSet.add(arr[1]);

Every child is stored.

Since root never becomes a child,

it will not exist inside this set.

Step 2: Identify Root

if (!childSet.contains(arr[0]))

This finds the node that only acts as parent.

That node becomes the root.

Step 3: Create Unique Tree Nodes

HashMap<Integer, TreeNode> map

Avoids duplicate node creation.

Each value maps to exactly one TreeNode.

Step 4: Connect Parent and Child

map.get(parent).left = map.get(child);

or

map.get(parent).right = map.get(child);

depending on:

isLeft

Dry Run

Input

[
[20,15,1],
[20,17,0],
[50,20,1],
[50,80,0],
[80,19,1]
]

Child Set

15, 17, 20, 80, 19

Root Detection

Check parents:

  1. 20 → exists in child set
  2. 50 → NOT in child set

So:

Root = 50

Tree Construction

50

  1. left → 20
  2. right → 80

20

  1. left → 15
  2. right → 17

80

  1. left → 19

Final tree becomes:

50
/ \
20 80
/ \ /
15 17 19

Time Complexity

Building Child Set

O(N)

Finding Root

O(N)

Constructing Tree

O(N)

Total Time Complexity

O(N)

Efficient for large constraints.

Space Complexity

HashMap + HashSet

O(N)

Why This Problem is Important

This problem teaches:

  1. Tree construction
  2. Parent-child mapping
  3. Root identification
  4. Efficient object reuse
  5. HashMap optimization

It is frequently asked in interviews because it combines multiple concepts in one clean implementation.

Common Mistakes

1. Creating Duplicate Nodes

Wrong:

new TreeNode(parent)

every time.

Always reuse nodes through HashMap.

2. Incorrect Root Detection

Remember:

Root never appears as child.

3. Forgetting Left vs Right Child

Always check:

isLeft

before attaching.

Interview Tips

In interviews explain:

We first identify the root using a child set, then use a HashMap to create and reuse TreeNode objects efficiently while connecting parent-child relationships.

This demonstrates:

  1. Strong data structure understanding
  2. Efficient memory usage
  3. Clean tree construction logic

Related Problems

Practice these next:

  1. Construct Binary Tree from Traversals
  2. Validate Binary Search Tree
  3. Serialize and Deserialize Binary Tree
  4. Lowest Common Ancestor
  5. Binary Tree Level Order Traversal

Conclusion

LeetCode 2196 is a clean and elegant binary tree construction problem.

The major insight is:

The root node is the only node that never appears as a child.

Combined with HashMap-based node reuse, we can construct the entire tree efficiently in linear time.

This is an excellent interview problem for mastering tree construction patterns.

Ai Assistant Kas