Skip to main content

Draw binary tree from preorder and inorder

Short answer

Root is first preorder symbol F. In inorder E A C K F H D B G, left of F is E A C K, right is H D B G. Recurse: left subtree root A (preorder A E K C) has left E, right K with left C; right subtree root D has left H, right G with left B.

Draw a binary tree: Preorder F A E K C D H G B; In order E A C K F H D B G

Method: The first symbol of the preorder is the root of the tree. Find it in the inorder sequence: symbols to its left form the left subtree, those to its right form the right subtree. Repeat for each subtree using the matching part of the preorder.

Preorder: F A E K C D H G B
Inorder: E A C K F H D B G

StepPreorder partInorder partRootLeft subtree (inorder)Right subtree (inorder)
1F A E K C D H G BE A C K F H D B GFE A C KH D B G
2A E K CE A C KAEC K
3K CC KKC–
4D H G BH D B GDHB G
5G BB GGB–

Leaf nodes E, C, H and B are the single-symbol parts.

Binary tree

            F
         /     \
        A       D
       / \     / \
      E   K   H   G
         /       /
        C       B

Verification

  • Preorder: F A E K C D H G B ✓.
  • Inorder: E A C K F H D B G ✓.
  • Postorder (extra): E C K A H B G D F.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 6 questions →