GateMCQSCIENTIFIC PREP LAB
PlanSign Up
BEGINNERCore Topic 4 min read 19 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!
Key Formulas Summary
Binary Tree Leaf-to-Degree-2 Theorem
n0=n2+1n_0 = n_2 + 1
Note: 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
Note: Where L = leaves and I = internal nodes
#Data Structures#Stacks#Queues#Linked Lists#Binary Trees#Traversals

Practice Questions on This Chapter (4)

Q1GATE CS DSAMCQEASY
+1M • -0.33

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?

⚡ Click an option above to verify instantly
Q2GATE CS DSAMCQEASY
+2M • -0.67

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

⚡ Click an option above to verify instantly
Q3GATE CS 2024 Set-2NATEASY
+2M • 0 Neg

Evaluate the postfix expression 10 5 + 60 6 / * 8 - using a standard operand stack.

Q4GATE CS 2017MCQMEDIUM
+2M • -0.67

The Preorder traversal of a Binary Search Tree (BST) is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which of the following is its Postorder traversal?

⚡ Click an option above to verify instantly
Previous TopicAVL Trees, Binary Heaps & Hashing Probe ComplexityNext Topic Advanced C Programming: Pointer Arithmetic, 2D Array Decay, Struct Padding & Bitwise Traps