∑GateAndNet
Practice MCQs
GATE CS / Programming & Data Structures
Edit
BEGINNER⭐ Must Know 4 min read 3 views

Stacks, Queues, Linked Lists & Binary Tree Properties

Infix/Postfix evaluation, Circular Queue full/empty modular arithmetic, Linked List pointer complexities, Binary Tree degree handshaking lemma, and unique tree reconstruction rules.

1. Stacks & Queues Complexity Nuances

  • Stack Applications: Function activation records, Infix-to-Postfix conversion (O(n)\mathcal{O}(n)), Postfix evaluation, Stock Span / Next Greater Element (O(n)\mathcal{O}(n) monotonic stack), and DFS.
  • Queue Using Two Stacks (S1,S2S_1, S_2): Enqueue pushes to S1S_1 in O(1)\mathcal{O}(1); Dequeue pops from S2S_2 (transferring S1→S2S_1 \to S_2 only when S2S_2 is empty)   ⟹  \implies Amortized O(1)\mathcal{O}(1) per operation, worst-case single dequeue Θ(n)\Theta(n).
  • Circular Array Queue (size NN, wasting 1 slot):
    • Empty: front == rear
    • Full: (rear + 1) % N == front (holds N−1N - 1 elements max unless an explicit count is maintained).

2. Singly vs Doubly Linked List Complexities

OperationSingly LL (Head only)Singly LL (Head + Tail)Doubly Linked List
Insert at Front / Delete at FrontΘ(1)\Theta(1)Θ(1)\Theta(1)Θ(1)\Theta(1)
Insert at EndΘ(n)\Theta(n)Θ(1)\Theta(1)Θ(1)\Theta(1) (with tail)
Delete Last NodeΘ(n)\Theta(n)Θ(n)\Theta(n) (still needs predecessor!)Θ(1)\Theta(1) (with tail)

3. Binary Tree Traversals & Unique Reconstruction

Given two traversals of a binary tree with nn distinct keys, when is the tree uniquely determined?

  • Inorder + Preorder   ⟹  \implies YES (Unique)
  • Inorder + Postorder   ⟹  \implies YES (Unique)
  • Inorder + Level-Order   ⟹  \implies YES (Unique)
  • Preorder + Postorder   ⟹  \implies NO in general — YES ONLY IF the binary tree is Full/Strict (every node has 00 or 22 children)!
  • BST Only: A single Preorder (or single Postorder) suffices to reconstruct a BST uniquely because sorting the keys automatically yields the Inorder traversal!
📐 Quick Revision Formula Sheet
Binary Tree Leaf-to-Degree-2 Theorem
n0=n2+1n_0 = n_2 + 1
💡 Number of leaf nodes (degree 0) equals number of degree-2 internal nodes + 1
Strict (Full) k-ary Tree Leaves
L=(k−1)I+1L = (k - 1)I + 1
💡 Where L = leaves and I = internal nodes
#Data Structures#Stacks#Queues#Linked Lists#Binary Trees#Traversals

🎯 Practice PYQs on This Topic (2)

Q1.GATE CS DSAMCQEASY
+1 Marks • -0.33 Neg

A binary tree has 2525 nodes with degree 22 (two children) and 1212 nodes with degree 11. How many leaf nodes (degree 00) does the binary tree contain?

Q2.GATE CS DSAMCQEASY
+2 Marks • -0.67 Neg

Evaluate the postfix (Reverse Polish) expression: 8 2 3 ^ / 2 3 * + 5 1 * - where ^ denotes exponentiation.

Previous TopicAVL Trees, Binary Heaps & Hashing Probe ComplexityNext Topic Asymptotic Notation & Master Theorem for Divide-and-Conquer