binary tree
integer array
tree conversion
data structures
algorithms

convert integer array into a binary tree

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting an integer array into a binary tree usually means interpreting the array in level order: the first element becomes the root, the next two become its children, the next four fill the next level, and so on. That is a natural representation for complete or nearly complete trees. The exact algorithm depends on whether the array is dense or whether it also uses placeholders such as None to represent missing nodes.

Level-Order Conversion For A Dense Array

If every array element corresponds to a real node, the index relationships are simple.

For a node at index i:

  • left child is at 2*i + 1
  • right child is at 2*i + 2

Here is a Python implementation.

python
1class Node:
2    def __init__(self, value):
3        self.value = value
4        self.left = None
5        self.right = None
6
7
8def array_to_tree(values, index=0):
9    if index >= len(values):
10        return None
11
12    node = Node(values[index])
13    node.left = array_to_tree(values, 2 * index + 1)
14    node.right = array_to_tree(values, 2 * index + 2)
15    return node
16
17
18def preorder(node):
19    if node is None:
20        return []
21    return [node.value] + preorder(node.left) + preorder(node.right)
22
23
24root = array_to_tree([1, 2, 3, 4, 5, 6, 7])
25print(preorder(root))

This produces a full binary tree in the same level order implied by the array.

When The Array Contains Missing Nodes

Real-world tree arrays often contain placeholders for absent children. In that case, the direct index formula still works, but you should stop node creation when the value is None.

python
1class Node:
2    def __init__(self, value):
3        self.value = value
4        self.left = None
5        self.right = None
6
7
8def array_to_tree_with_none(values, index=0):
9    if index >= len(values) or values[index] is None:
10        return None
11
12    node = Node(values[index])
13    node.left = array_to_tree_with_none(values, 2 * index + 1)
14    node.right = array_to_tree_with_none(values, 2 * index + 2)
15    return node
16
17
18root = array_to_tree_with_none([1, 2, 3, None, 5, 6, None])
19print(root.left.right.value)

This is the right pattern when the input format describes a sparse tree.

Queue-Based Construction Is Another Option

A queue-based algorithm can be easier to reason about if you want to build the tree iteratively instead of recursively.

python
1from collections import deque
2
3class Node:
4    def __init__(self, value):
5        self.value = value
6        self.left = None
7        self.right = None
8
9
10def build_tree(values):
11    if not values:
12        return None
13
14    root = Node(values[0])
15    queue = deque([root])
16    i = 1
17
18    while queue and i < len(values):
19        current = queue.popleft()
20
21        if i < len(values):
22            current.left = Node(values[i])
23            queue.append(current.left)
24            i += 1
25
26        if i < len(values):
27            current.right = Node(values[i])
28            queue.append(current.right)
29            i += 1
30
31    return root

This version mirrors the human description of filling the tree level by level.

Binary Tree Versus Binary Search Tree

An integer array does not automatically become a binary search tree just because it contains numbers. If you use the level-order array mapping above, you are building a binary tree, not enforcing BST ordering.

If you want a BST, the algorithm is different. You would insert values one by one according to BST rules instead of placing them strictly by array index.

Common Pitfalls

The most common mistake is assuming every array-to-tree conversion should create a binary search tree. Level-order conversion creates only a binary tree structure unless you enforce additional ordering logic.

Another issue is forgetting to handle missing nodes. If the input format uses None as a placeholder, skipping that case produces the wrong tree shape.

It is also easy to mix zero-based and one-based index formulas. The usual child formulas shown here assume zero-based arrays.

Finally, recursion is clean, but very deep trees may hit recursion limits in some languages. An iterative queue-based build can be safer in those cases.

Summary

  • A level-order array naturally maps to a binary tree using index relationships.
  • For zero-based arrays, children of index i are at 2*i + 1 and 2*i + 2.
  • Use None handling when the array represents a sparse tree.
  • Queue-based construction is a good iterative alternative to recursion.
  • Do not confuse binary tree conversion with building a binary search tree.

Course illustration
Course illustration

All Rights Reserved.