All Exams Test series for 1 year @ ₹349 only
Question

The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?

The correct answer is

15, 10, 23, 25, 20, 35, 42, 39, 30

Understanding Binary Search Trees and Traversal Sequences

A Binary Search Tree (BST) is a special type of binary tree where for every node, all elements in its left subtree are smaller than the node, and all elements in its right subtree are larger than the node. This property makes BSTs efficient for searching, insertion, and deletion operations.

Tree traversals are methods for visiting all nodes in a tree exactly once. Common traversal methods include:

  • Preorder Traversal: Visit the root node, then recursively traverse the left subtree, then recursively traverse the right subtree (Root, Left, Right).
  • Inorder Traversal: Recursively traverse the left subtree, visit the root node, then recursively traverse the right subtree (Left, Root, Right). For a BST, inorder traversal always gives a sorted sequence of elements.
  • Postorder Traversal: Recursively traverse the left subtree, then recursively traverse the right subtree, then visit the root node (Left, Right, Root).

Reconstructing the BST from Preorder Traversal

We are given the preorder traversal sequence: 30, 20, 10, 15, 25, 23, 39, 35, 42.

In a preorder traversal (Root, Left, Right), the first element is always the root of the tree (or subtree being considered). For a BST, elements smaller than the root are in the left subtree, and elements larger than the root are in the right subtree. We can use this property to reconstruct the tree recursively.

  1. The first element, 30, is the root of the tree.
  2. Scan the rest of the preorder sequence (20, 10, 15, 25, 23, 39, 35, 42). Elements smaller than 30 belong to the left subtree, and elements larger than 30 belong to the right subtree.
    • Elements < 30: 20, 10, 15, 25, 23
    • Elements > 30: 39, 35, 42
    The preorder sequence for the left subtree is 20, 10, 15, 25, 23. The preorder sequence for the right subtree is 39, 35, 42.
  3. Recursively build the left subtree using its preorder sequence (20, 10, 15, 25, 23):
    • 20 is the root of the left subtree.
    • Scan the remaining sequence (10, 15, 25, 23). Elements < 20 (10, 15) form the left part, and elements > 20 (25, 23) form the right part within the left subtree's sequence.
    • Preorder for 20's left subtree: 10, 15.
    • Preorder for 20's right subtree: 25, 23.
  4. Recursively build the left subtree of node 20 using (10, 15):
    • 10 is the root.
    • Scan (15). Elements < 10: none. Elements > 10: 15.
    • 15 is the right child of 10.
  5. Recursively build the right subtree of node 20 using (25, 23):
    • 25 is the root.
    • Scan (23). Elements < 25: 23. Elements > 25: none.
    • 23 is the left child of 25.
  6. Recursively build the right subtree of node 30 using (39, 35, 42):
    • 39 is the root of the right subtree.
    • Scan the remaining sequence (35, 42). Elements < 39: 35. Elements > 39: 42.
    • 35 is the left child of 39.
    • 42 is the right child of 39.

Based on the reconstruction steps, the structure of the Binary Search Tree is:

      30
     /  \
    20   39
   / \   / \
  10  25 35 42
   \  /
   15 23

Performing Postorder Traversal

Postorder traversal visits nodes in the order: Left subtree, Right subtree, Root.

Starting from the root (30):

  1. Traverse the left subtree (rooted at 20):
    • Traverse the left subtree of 20 (rooted at 10):
      • Traverse the left subtree of 10 (empty).
      • Traverse the right subtree of 10 (node 15). Visit 15.
      • Visit node 10.
      • Sequence: 15, 10
    • Traverse the right subtree of 20 (rooted at 25):
      • Traverse the left subtree of 25 (node 23). Visit 23.
      • Traverse the right subtree of 25 (empty).
      • Visit node 25.
      • Sequence: 15, 10, 23, 25
    • Visit node 20.
    • Sequence: 15, 10, 23, 25, 20
  2. Traverse the right subtree (rooted at 39):
    • Traverse the left subtree of 39 (node 35). Visit 35.
    • Traverse the right subtree of 39 (node 42). Visit 42.
    • Visit node 39.
    • Sequence: 15, 10, 23, 25, 20, 35, 42, 39
  3. Visit the root node 30.
  4. Final Postorder Sequence: 15, 10, 23, 25, 20, 35, 42, 39, 30.

Binary Search Tree Traversal Summary

Traversal Type Order
Preorder Root, Left, Right
Inorder Left, Root, Right
Postorder Left, Right, Root

The postorder traversal sequence obtained is 15, 10, 23, 25, 20, 35, 42, 39, 30.

Revision Table: BST Properties and Traversals

Concept Description Traversal Order
Binary Tree Each node has at most two children.
Binary Search Tree (BST) Left child < Parent < Right child property holds for all nodes.
Preorder Traversal Used to create a copy of the tree. Root, Left, Right
Inorder Traversal Results in a sorted sequence in a BST. Left, Root, Right
Postorder Traversal Useful for deleting nodes or subtrees (delete children before parent). Left, Right, Root

Additional Information: Tree Reconstruction

Knowing one traversal sequence is generally not enough to reconstruct a unique binary tree. However, if the tree is a Binary Search Tree, the preorder traversal sequence is sufficient because the BST property helps in identifying which elements belong to the left and right subtrees. Similarly, a postorder traversal sequence is also sufficient for reconstructing a BST. If you have both inorder and either preorder or postorder, you can reconstruct any general binary tree, not just a BST.

The key for BST reconstruction from preorder is that the root splits the remaining sequence into elements < root (left subtree) and elements > root (right subtree), preserving their relative order from the original preorder sequence.

Was this answer helpful?

Important Questions from Binary Search Tree - Teaching

  1. Suppose that we have numbers between 1 and 1000 in a binary search tree and we want to search for the number 365. Which of the following sequences could not be the sequence of nodes examined ?

  2. Suppose that we have numbers between 1 and 1,000 in a binary search tree and want to search for the number 364. Which of the following sequences could not be the sequence of nodes examined ?

  3. In a ____the left and right subtrees for any given node, differ in height by more than one.

  4. Which of the following description(s) is/are true:

    A. Red Black Tree - Guarantees worst case of O(log n) time for search, insert & delete.

    B. Trie - Used for efficient prefix - based searches.

    C. AVL Tree - self balancing binary search tree with looser balance criteria than a Red Black Tree.

    D. B- Tree -  Allows efficient search, inert, delete operations in disk based system.

    Choose the correct answer from the options given below:

  5. __________ is a Self Balancing binary search tree, where the path from the root to the furthest leaf is no more than twice as long as the path from the root to nearest leaf.

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App