↓Skip to main content
  1. LeetCode/

LeetCode 2196: Create Binary Tree From Descriptions

·2 mins· ·
LeetCode Medium Tree Binary-Tree Hash-Map
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-06-26

  • Submissions: Accepted (first try)

Approach
#

Two tasks:

  1. Find the root: the only value that appears but is never anyone’s child.
  2. Build the tree: attach each child to the left or right of its parent as described.

My Solution: a parent → children table, then build recursively
#

from collections import defaultdict

class Solution:
    def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
        parent_map = defaultdict(list)
        child_set = set()
        for parent, child, isLeft in descriptions:
            parent_map[parent].append((child, isLeft))
            child_set.add(child)

        root = (set(parent_map.keys()) - child_set).pop()

        def build_tree(node):
            if node.val not in parent_map:
                return node
            for child, isLeft in parent_map[node.val]:
                if isLeft:
                    node.left = build_tree(TreeNode(child))
                else:
                    node.right = build_tree(TreeNode(child))
            return node

        return build_tree(TreeNode(root))

Correct, with one risk: there can be 10^4 descriptions, and a chain-shaped tree makes the recursion 10^4 deep, past Python’s default limit.

Improved: a dict of nodes, linked in one loop
#

No recursion needed: map each value to its node and link nodes as you read each description.

class Solution:
    def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
        nodes = {}
        children = set()
        for parent, child, is_left in descriptions:
            if parent not in nodes:
                nodes[parent] = TreeNode(parent)
            if child not in nodes:
                nodes[child] = TreeNode(child)
            if is_left:
                nodes[parent].left = nodes[child]
            else:
                nodes[parent].right = nodes[child]
            children.add(child)

        root = next(v for v in nodes if v not in children)
        return nodes[root]

Complexity
#

Time O(n), space O(n), where n is the number of descriptions.

Mistakes to Avoid
#

  • Calling TreeNode(...) every time a value appears: one value becomes several different nodes and the tree falls apart.
  • Recursive building that hits the recursion limit on a chain-shaped tree.

Interview Explanation
#

I map each value to a single node in a dict, scan the descriptions once to attach each child to its parent’s left or right, and record every child I see. The root is the only value not in the child set. There is no recursion, so depth is not an issue, and time and space are O(n).

Related

LeetCode 1339: Maximum Product of Splitted Binary Tree
·2 mins
LeetCode Medium Tree Binary-Tree Dfs
LeetCode 1339 note: compute the total, enumerate every subtree sum, and leave the input untouched
LeetCode 865: Smallest Subtree with all the Deepest Nodes
·2 mins
LeetCode Medium Tree Binary-Tree Bfs Dfs Lca
LeetCode 865 note: BFS to the last level and climb together, or one DFS returning (depth, node)
LeetCode 1161: Maximum Level Sum of a Binary Tree
·2 mins
LeetCode Medium Tree Binary-Tree Bfs
LeetCode 1161 note: pushing None into the next level creates a fake level with sum 0
LeetCode 1008: Construct Binary Search Tree from Preorder Traversal
·2 mins
LeetCode Medium Tree Bst Binary-Tree Recursion
LeetCode 1008 note: splitting the array is O(n^2); recursing with an upper bound is O(n)
LeetCode 958: Check Completeness of a Binary Tree
·2 mins
LeetCode Medium Tree Binary-Tree Bfs
LeetCode 958 note: completeness is a global property, so check with BFS whether any node follows the first gap
LeetCode 103: Binary Tree Zigzag Level Order Traversal
·1 min
LeetCode Medium Tree Breadth-First-Search Binary-Tree
LeetCode 103 note: a normal level-order traversal with every odd level reversed gives the zigzag