LearnFrenzy
Sign in Take a free test

DSA & Algorithms · Topic 8 of 12

Trees and BST: questions with answers

Traversals, height and leaves, BST search, insert and delete, building trees from traversals, LCA, diameter, AVL rotations, tries and segment trees.

Questions
45
Timed sets
3
Answers run or computed
33
Your accuracy
–

What this topic covers

17 subtopics
  • Traversals8
  • Height and depth5
  • Building trees4
  • AVL trees3
  • BST basics3
  • BST search3
  • Balanced trees2
  • BST insertion2
  • BST order statistics2
  • Library trees2
  • Range trees2
  • Root-to-leaf paths2
Show all 17 subtopics
  • Tree shapes2
  • Tries2
  • BST deletion1
  • Lowest common ancestor1
  • Tree transformations1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

Trees are drawn on this page, so you can read them the way you would on paper. Most tree questions are traversals: preorder (node, left, right), inorder (left, node, right – sorted for a BST), postorder (left, right, node) and level order (breadth-first). A binary search tree keeps smaller keys on the left and larger on the right, so search, insert and delete follow one path – O(h), which is O(log n) only when the tree is balanced. Every traversal and every tree in the answers was produced by a checker program.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Traversals Beginner Practice Answer computed

What is the inorder traversal of this tree?

Binary search tree
134678101314
831647101413
831016144713
134678101314
147631314108
Show answer and explanation
Correct answer C

Explanation

Left subtree, node, right subtree – which for a BST lists the keys in sorted order.

Why the other options are wrong

  • AThat is preorder.
  • BThat is level order.
  • DThat is postorder.
Question 2 Building trees Intermediate Practice Answer computed

A binary tree has preorder 3 9 20 15 7 and inorder 9 3 15 20 7. What is its postorder?

7152093
9315207
9157203
1572093
Show answer and explanation
Correct answer C

Explanation

Preorder's first value (3) is the root; in the inorder it splits left [9] from right [15, 20, 7]. The right part's preorder starts with 20, which splits 15 from 7. Postorder: 9, 15, 7, 20, 3.

Why the other options are wrong

  • AThe left subtree comes before the right.
  • BThat is the inorder given.
  • D9, the whole left subtree, comes first.
Question 3 Building trees Advanced Practice

Preorder 1 2 and postorder 2 1 describe a two-node tree. Why can the tree not be determined uniquely?

The traversals contradict each other
Postorder must end with the smallest key
Two-node trees are always left-leaning
2 could be the left child or the right child of 1 – preorder + postorder fix the tree only when every node has 0 or 2 children
Show answer and explanation
Correct answer D

Explanation

Both trees give the same pair of sequences. Inorder resolves it, because it tells which side of the root each key is on. GATE often asks which traversal pairs are sufficient.

Why the other options are wrong

  • AThey are consistent with two different trees.
  • BPostorder ends with the root.
  • CNo such rule.
42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DSA & Algorithms

All 12 topics

Topics, levels and full mocks
  1. 01 Time and Space Complexity45 questions · 3 levels
  2. 02 Arrays and Strings45 questions · 3 levels
  3. 03 Linked Lists45 questions · 3 levels
  4. 04 Stacks and Queues45 questions · 3 levels
  5. 05 Hashing45 questions · 3 levels
  6. 06 Recursion and Backtracking45 questions · 3 levels
  7. 07 Sorting and Searching45 questions · 3 levels
  8. 08 Trees and BST45 questions · 3 levels
  9. 09 Heaps and Priority Queues45 questions · 3 levels
  10. 10 Graphs45 questions · 3 levels
  11. 11 Dynamic Programming45 questions · 3 levels
  12. 12 Greedy and Bit Manipulation45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links