# Tree Questions for University Exams in Bangladesh: Written, Lab & Viva (With Answers)

> Tree questions for university exams in Bangladesh with model answers: traversals, building a tree from traversals, recursive and iterative algorithms, viva.

- Canonical URL: https://shafinchowdhury.dev/posts/tree-questions-university-exams-bangladesh/
- Author: Shafin Chowdhury (https://shafinchowdhury.dev/about/)
- Published: 2026-12-04T03:20:00.000Z
- Updated: 2026-12-04T03:20:00.000Z
- Reading Time: 20 min read
- Tags: cpp, data-structures, trees, binary-tree, exam-preparation

---

import Diagram from "@/components/Diagram.astro";
import TraversalPlayer from "@/components/TraversalPlayer.astro";

Tree questions in a Data Structures course follow a small number of patterns: **define and differentiate** (tree, binary tree, full/complete/perfect), **traverse a given diagram** (preorder, inorder, postorder), **rebuild a tree from two traversals**, **write an algorithm** (recursive or iterative, often as pseudocode), **trace a recursive function**, and **analyse complexity**. This guide covers each pattern with model answers you can write in an exam script, then gives a full question bank for written, lab and viva exams.

> **Tree & Graph series, step 5 of 8.** Concepts are taught in [Trees, Binary Trees & Traversals](/posts/tree-binary-tree-traversals/); this article is the exam-practice companion. Next: [BST Questions for University Exams](/posts/bst-questions-university-exams-bangladesh/).

---

## The Exam Tree Used Throughout

<Diagram
  src="tree-questions-university-exams-bangladesh/exam-tree-question"
  alt="Hand-drawn style exam diagram of a binary tree with root A. A has children B and C. B has children D and E. D has only a right child G. C has only a left child F. F has children H and I."
/>

---

## Part 1: Definitions and Terminology

### Q1. Define a tree with an example

**Model answer.** A tree is a finite set of one or more nodes such that:

1. There is one specially designated node called the **root**.
2. The remaining nodes are partitioned into $n \ge 0$ disjoint sets $T_1, T_2, \ldots, T_n$. Each of these is itself a tree, called a **subtree** of the root.

Equivalently, a tree is a connected graph with no cycles. A tree with $n$ nodes has $n - 1$ edges.

*Example:* the exam tree above, with root A and subtrees rooted at B and C. A real-world example is a folder hierarchy.

### Binary tree terminology (with the exam tree)

| Term | Definition | From the exam tree |
| --- | --- | --- |
| Root | Node with no parent | A |
| Leaf / terminal node | Node with no children | E, G, H, I |
| Internal / non-terminal node | Node with at least one child | A, B, C, D, F |
| Parent, child | Direct predecessor, direct successor | F is the parent of H and I |
| Siblings | Children of the same parent | H and I; B and C |
| Ancestors of a node | All nodes on the path from the root to it | Ancestors of H: A, C, F |
| Descendants | All nodes in its subtree except itself | Descendants of B: D, E, G |
| Degree of a node | Number of children | deg(A) = 2, deg(C) = 1, deg(E) = 0 |
| Depth / level of a node | Edges from the root | depth(G) = 3 |
| Height of the tree | Longest root-to-leaf path in edges | 3 (A–B–D–G or A–C–F–H) |
| Path length | Number of edges in the path | A to I: 3 |

### Q2. Differentiate full, complete and perfect binary trees

| Feature | Full (strict) | Complete | Perfect |
| --- | --- | --- | --- |
| Rule | Every node has 0 or 2 children | All levels full except possibly the last; last level filled left to right | All internal nodes have 2 children and all leaves at the same level |
| Can a node have 1 child? | No | Yes (at most one such node, on the second-last level) | No |
| Nodes for height $h$ | between $2h + 1$ and $2^{h+1} - 1$ | between $2^h$ and $2^{h+1} - 1$ | exactly $2^{h+1} - 1$ |
| Relationship | — | — | Every perfect tree is both full and complete |

<Diagram
  src="tree-binary-tree-traversals/binary-tree-types"
  alt="Five binary trees: full but not complete, complete but not full, perfect, balanced but not complete, and skewed."
/>

**Applying this to the exam tree:**

- **Full?** No. C and D have one child each.
- **Complete?** No. D has no left child, while nodes exist further right on the next level.
- **Perfect?** No.

> Some older textbooks use "full binary tree" to mean what this table calls **perfect**. If your course textbook does, follow it, and mention the alternative name in one line.

### Properties of binary trees

With levels numbered from 0 at the root and height $h$ in edges:

1. The maximum number of nodes on level $i$ is $2^i$.
2. The maximum number of nodes in a binary tree of height $h$ is $2^{h+1} - 1$.
3. For any non-empty binary tree, $n_0 = n_2 + 1$, where $n_0$ = number of leaves and $n_2$ = number of nodes with two children.
   - *Check on the exam tree:* leaves E, G, H, I give $n_0 = 4$.
   - Nodes with two children A, B, F give $n_2 = 3$. ✓
4. A binary tree with $n$ nodes has exactly $n - 1$ edges and $n + 1$ null links.
5. The minimum height of a binary tree with $n$ nodes is $\lceil \log_2(n+1) \rceil - 1$.

**Proof of property 3 (a common 4–5 mark question).**

1. Let $n_1$ be the number of nodes with one child. Total nodes: $n = n_0 + n_1 + n_2$.
2. Every node except the root has exactly one incoming edge, so edges $= n - 1$.
3. Counting edges from the parent side, edges $= n_1 + 2n_2$.
4. Therefore $n_0 + n_1 + n_2 - 1 = n_1 + 2n_2$, which gives $n_0 = n_2 + 1$. ∎

### Binary tree representations

**1. Linked representation.** Each node has three fields: `LEFT`, `INFO`, `RIGHT`.

```cpp excerpt
struct Node {
    char data;
    Node* left;
    Node* right;
    explicit Node(char value) : data(value), left(nullptr), right(nullptr) {}
};
```

**2. Sequential (array) representation.** Store the root at index 1. For a node at index $k$:

- left child at $2k$
- right child at $2k + 1$
- parent at $\lfloor k/2 \rfloor$

(With 0-based indexing: $2k + 1$, $2k + 2$ and $\lfloor (k-1)/2 \rfloor$.)

The exam tree in a 1-based array:

| Index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Value | A | B | C | D | E | F | – | – | G | – | – | H | I |

- G is the right child of D (index 4), so it sits at $2 \cdot 4 + 1 = 9$.
- H and I are the children of F (index 6), so they sit at 12 and 13.
- Thirteen slots hold nine nodes. For a skewed tree the waste grows exponentially, which is the main disadvantage of the array method.
- A **complete** tree has no gaps at all, which is why heaps use this method.

| | Linked | Array |
| --- | --- | --- |
| Memory | Two pointers per node | Can waste many empty slots |
| Insert / delete | Easy | Hard (may need resizing) |
| Find parent | Needs an extra pointer | $\lfloor k/2 \rfloor$, $O(1)$ |
| Best for | General trees | Complete trees, heaps |

---

## Part 2: Traversal Questions

### Q3. Write the preorder, inorder and postorder of the given tree

**Method that never fails in an exam:** write the rule at the top of your answer, then expand one subtree at a time.

**Preorder (Root, Left, Right):**
A, then preorder(B) = B D G E, then preorder(C) = C F H I.
→ **A B D G E C F H I**

**Inorder (Left, Root, Right):**
inorder(B) = D G B E, then A, then inorder(C) = H F I C.
→ **D G B E A H F I C**

**Postorder (Left, Right, Root):**
postorder(B) = G D E B, then postorder(C) = H I F C, then A.
→ **G D E B H I F C A**

**Level order:** **A B C D E F G H I**

**Quick self-checks:**

- Preorder starts with the root.
- Postorder ends with the root.
- In inorder, every left-subtree node comes before A, and every right-subtree node comes after it.

<TraversalPlayer
  tree={["A", "B", "C", "D", "E", "F", null, null, "G", null, null, "H", "I"]}
  orders={["preorder", "inorder", "postorder", "levelorder"]}
  caption="Step through the exam tree's traversals to check your hand-written answers."
/>

### Expression trees (a favourite 3–5 mark question)

<Diagram
  src="tree-questions-university-exams-bangladesh/expression-tree"
  alt="Expression tree for (a + b) * (c - d): root *, left child + with children a and b, right child - with children c and d."
/>

Operators are internal nodes, operands are leaves.

- **Preorder** gives prefix (Polish) notation: `* + a b - c d`
- **Inorder** gives infix (without brackets): `a + b * c - d`, so you must add brackets for each subtree to keep the meaning: `(a + b) * (c - d)`
- **Postorder** gives postfix (reverse Polish) notation: `a b + c d - *`

To evaluate postfix, use a [stack](/posts/understanding-stack-in-cpp/):

- Push each operand.
- On an operator, pop two operands, apply the operator, and push the result.

---

## Part 3: Construct a Binary Tree From Traversals

**Rule to remember:**

- Preorder or postorder tells you **which node is the root**.
- Inorder tells you **which nodes are on its left and right**.
- So you need **inorder plus one of the other two** (with distinct values). Preorder + postorder alone is not enough in general.

### Q4. Construct the binary tree whose preorder is `A B D E C F G` and inorder is `D B E A F C G`.

<Diagram
  src="tree-questions-university-exams-bangladesh/construct-from-preorder-inorder"
  alt="Step 1: the inorder sequence D B E A F C G with A highlighted, splitting it into the left subtree D B E and right subtree F C G. Result: the perfect tree A with children B and C, B with children D and E, C with children F and G."
/>

1. First preorder element **A** is the root. In inorder, `D B E` lie left of A and `F C G` right of A.
2. Left subtree: its preorder is the next three preorder elements, `B D E`; its inorder is `D B E`. Root **B**; left `D`, right `E`.
3. Right subtree: preorder `C F G`, inorder `F C G`. Root **C**; left `F`, right `G`.
4. **Verify:** the inorder of your drawn tree must reproduce the given inorder. `D B E A F C G` ✓.

### Q5. Construct the tree from postorder `D E B F G C A` and inorder `D B E A F C G`.

<Diagram
  src="tree-questions-university-exams-bangladesh/construct-from-postorder-inorder"
  alt="The tree built from the postorder D E B F G C A and inorder D B E A F C G: root A with children B and C, B with children D and E, C with children F and G."
/>

1. The **last** postorder element is the root (A).
2. Split inorder at A, as before.
3. Process postorder **from the right**: the element before A (C) is the root of the **right** subtree, so build the right subtree first.
4. The result is the same tree as Q4, which shows that both methods agree.

### Q6. Construct the tree from preorder `A B D G E C F H I` and inorder `D G B E A H F I C`.

Answer: the exam tree at the top of this page.

---

## Part 4: Algorithm Writing (Recursive, Iterative, Pseudocode)

### Recursive traversal algorithms

```text
PREORDER(T)
1. If T = NULL, return.
2. Visit INFO[T].
3. PREORDER(LEFT[T]).
4. PREORDER(RIGHT[T]).

INORDER(T)
1. If T = NULL, return.
2. INORDER(LEFT[T]).
3. Visit INFO[T].
4. INORDER(RIGHT[T]).

POSTORDER(T)
1. If T = NULL, return.
2. POSTORDER(LEFT[T]).
3. POSTORDER(RIGHT[T]).
4. Visit INFO[T].
```

**Base case:** an empty subtree (step 1). **Recursive case:** steps 2–4, with two recursive calls, each on a strictly smaller subtree, so the recursion always terminates.

### Iterative (non-recursive) traversal algorithms using a stack

These are classic 5–8 mark questions: "Write a non-recursive algorithm for inorder traversal."

```text
ITERATIVE-PREORDER(ROOT)
1. If ROOT = NULL, return.
2. PUSH(ROOT).
3. While stack is not empty:
     PTR := POP()
     Visit INFO[PTR]
     If RIGHT[PTR] ≠ NULL, PUSH(RIGHT[PTR])    // right first,
     If LEFT[PTR]  ≠ NULL, PUSH(LEFT[PTR])     // so left is popped first

ITERATIVE-INORDER(ROOT)
1. PTR := ROOT.
2. Repeat while PTR ≠ NULL or stack is not empty:
     While PTR ≠ NULL:            // go as far left as possible
         PUSH(PTR); PTR := LEFT[PTR]
     PTR := POP()
     Visit INFO[PTR]
     PTR := RIGHT[PTR]

ITERATIVE-POSTORDER(ROOT)       // two-stack method
1. If ROOT = NULL, return.
2. PUSH ROOT onto S1.
3. While S1 is not empty:
     PTR := POP(S1); PUSH PTR onto S2
     If LEFT[PTR]  ≠ NULL, PUSH LEFT[PTR]  onto S1
     If RIGHT[PTR] ≠ NULL, PUSH RIGHT[PTR] onto S1
4. Pop and visit every node of S2.
```

Why does the two-stack postorder work? S1 produces the order Root, Right, Left; pushing that onto S2 and popping reverses it to Left, Right, Root.

### Height, number of nodes, leaves and internal nodes

```text
HEIGHT(T)                   // edges; empty tree = -1
1. If T = NULL, return -1.
2. Return 1 + MAX(HEIGHT(LEFT[T]), HEIGHT(RIGHT[T])).

COUNT(T)
1. If T = NULL, return 0.
2. Return 1 + COUNT(LEFT[T]) + COUNT(RIGHT[T]).

LEAVES(T)
1. If T = NULL, return 0.
2. If LEFT[T] = NULL and RIGHT[T] = NULL, return 1.
3. Return LEAVES(LEFT[T]) + LEAVES(RIGHT[T]).

INTERNAL(T) = COUNT(T) - LEAVES(T)
```

### Complete C++ program (lab exam ready)

This program builds the exam tree and prints everything asked in this section, using both recursive and iterative traversals.

```cpp
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;

struct Node {
    char data;
    Node* left;
    Node* right;
    explicit Node(char value) : data(value), left(nullptr), right(nullptr) {}
};

void preorder(Node* t) {
    if (!t) {
        return;
    }
    cout << t->data << ' ';
    preorder(t->left);
    preorder(t->right);
}
void inorder(Node* t) {
    if (!t) {
        return;
    }
    inorder(t->left);
    cout << t->data << ' ';
    inorder(t->right);
}
void postorder(Node* t) {
    if (!t) {
        return;
    }
    postorder(t->left);
    postorder(t->right);
    cout << t->data << ' ';
}

void preorderIterative(Node* root) {
    if (!root) {
        return;
    }
    stack<Node*> st;
    st.push(root);
    while (!st.empty()) {
        Node* p = st.top();
        st.pop();
        cout << p->data << ' ';
        if (p->right) {
            st.push(p->right); // right first so left is processed first
        }
        if (p->left) {
            st.push(p->left);
        }
    }
}

void inorderIterative(Node* root) {
    stack<Node*> st;
    Node* p = root;
    while (p || !st.empty()) {
        while (p) { // go as far left as possible
            st.push(p);
            p = p->left;
        }
        p = st.top();
        st.pop();
        cout << p->data << ' ';
        p = p->right;
    }
}

void postorderIterative(Node* root) {
    if (!root) {
        return;
    }
    stack<Node*> s1, s2;
    s1.push(root);
    while (!s1.empty()) {
        Node* p = s1.top();
        s1.pop();
        s2.push(p);                      // s2 receives Root, Right, Left order
        if (p->left) {
            s1.push(p->left);
        }
        if (p->right) {
            s1.push(p->right);
        }
    }
    while (!s2.empty()) { // reversed: Left, Right, Root
        cout << s2.top()->data << ' ';
        s2.pop();
    }
}

void levelOrder(Node* root) {
    if (!root) {
        return;
    }
    queue<Node*> q;
    q.push(root);
    while (!q.empty()) {
        Node* p = q.front();
        q.pop();
        cout << p->data << ' ';
        if (p->left) {
            q.push(p->left);
        }
        if (p->right) {
            q.push(p->right);
        }
    }
}

int height(Node* t) {
    return t ? 1 + max(height(t->left), height(t->right)) : -1;
}
int countNodes(Node* t) {
    return t ? 1 + countNodes(t->left) + countNodes(t->right) : 0;
}
int countLeaves(Node* t) {
    if (!t) {
        return 0;
    }
    if (!t->left && !t->right) {
        return 1;
    }
    return countLeaves(t->left) + countLeaves(t->right);
}

void destroy(Node* t) {
    if (!t) {
        return;
    }
    destroy(t->left);
    destroy(t->right);
    delete t;
}

int main() {
    // Build the exam tree: connect each node to its parent.
    Node* A = new Node('A');
    A->left = new Node('B');
    A->right = new Node('C');
    A->left->left = new Node('D');
    A->left->right = new Node('E');
    A->left->left->right = new Node('G');
    A->right->left = new Node('F');
    A->right->left->left = new Node('H');
    A->right->left->right = new Node('I');

    cout << "Preorder  (recursive): ";
    preorder(A);
    cout << '\n';
    cout << "Preorder  (iterative): ";
    preorderIterative(A);
    cout << '\n';
    cout << "Inorder   (recursive): ";
    inorder(A);
    cout << '\n';
    cout << "Inorder   (iterative): ";
    inorderIterative(A);
    cout << '\n';
    cout << "Postorder (recursive): ";
    postorder(A);
    cout << '\n';
    cout << "Postorder (iterative): ";
    postorderIterative(A);
    cout << '\n';
    cout << "Level order:           ";
    levelOrder(A);
    cout << '\n';

    int n = countNodes(A), leaves = countLeaves(A);
    cout << "Height = " << height(A) << ", nodes = " << n
         << ", leaves = " << leaves << ", internal = " << n - leaves << '\n';
    destroy(A);
}
```

**Output:**

```text
Preorder  (recursive): A B D G E C F H I
Preorder  (iterative): A B D G E C F H I
Inorder   (recursive): D G B E A H F I C
Inorder   (iterative): D G B E A H F I C
Postorder (recursive): G D E B H I F C A
Postorder (iterative): G D E B H I F C A
Level order:           A B C D E F G H I
Height = 3, nodes = 9, leaves = 4, internal = 5
```

---

## Part 5: Tracing Recursive Functions

### Q7. Show the sequence of calls made by `inorder(1)` on the tree `1(2(4, 5), 3)` and give the output.

<Diagram
  src="tree-questions-university-exams-bangladesh/trace-inorder-calls"
  alt="Tree 1 with children 2 and 3, and 2 with children 4 and 5. Badges show inorder positions: 4 first, then 2, 5, 1, 3. The output row reads 4 2 5 1 3."
/>

| # | Call / action | Call stack after (bottom → top) | Output so far |
| --- | --- | --- | --- |
| 1 | `inorder(1)` → goes left | 1 | |
| 2 | `inorder(2)` → goes left | 1, 2 | |
| 3 | `inorder(4)`; left is NULL; **print 4**; right is NULL; return | 1, 2 | 4 |
| 4 | back in 2: **print 2**; go right | 1, 2 | 4 2 |
| 5 | `inorder(5)`: **print 5**; return | 1, 2 | 4 2 5 |
| 6 | 2 returns; back in 1: **print 1**; go right | 1 | 4 2 5 1 |
| 7 | `inorder(3)`: **print 3**; return | 1 | 4 2 5 1 3 |
| 8 | 1 returns | (empty) | **4 2 5 1 3** |

- The maximum stack depth is 3 (calls for 1, 2 and then 4 or 5), plus the NULL calls, which return immediately.
- This is why the space complexity is $O(h)$.

### Q8. What does `fun(root)` print for the exam tree? What does `mystery(root)` return?

```cpp nocompile
void fun(Node* t) {
    if (t == nullptr) {
        return;
    }
    fun(t->right);
    cout << t->data << ' ';
    fun(t->left);
}

int mystery(Node* t) {
    if (t == nullptr) {
        return 0;
    }
    int self = ((t->left == nullptr) != (t->right == nullptr)) ? 1 : 0;
    return self + mystery(t->left) + mystery(t->right);
}
```

**Answer.**

- `fun` is **reverse inorder** (Right, Root, Left), so it prints the inorder sequence backwards: **C I F H A E B G D**.
- `mystery` adds 1 for every node where exactly one child is null, so it counts nodes with **exactly one child**: C and D → **2**.

You can verify both with this program:

```cpp
#include <iostream>
using namespace std;

struct Node {
    char data;
    Node* left;
    Node* right;
    explicit Node(char value) : data(value), left(nullptr), right(nullptr) {}
};

void fun(Node* t) {
    if (t == nullptr) {
        return;
    }
    fun(t->right);
    cout << t->data << ' ';
    fun(t->left);
}

int mystery(Node* t) {
    if (t == nullptr) {
        return 0;
    }
    int self = ((t->left == nullptr) != (t->right == nullptr)) ? 1 : 0;
    return self + mystery(t->left) + mystery(t->right);
}

void destroy(Node* t) {
    if (!t) {
        return;
    }
    destroy(t->left);
    destroy(t->right);
    delete t;
}

int main() {
    Node* A = new Node('A');
    A->left = new Node('B');
    A->right = new Node('C');
    A->left->left = new Node('D');
    A->left->right = new Node('E');
    A->left->left->right = new Node('G');
    A->right->left = new Node('F');
    A->right->left->left = new Node('H');
    A->right->left->right = new Node('I');

    fun(A);
    cout << "\nmystery = " << mystery(A) << '\n';
    destroy(A);
}
```

**Output:**

```text
C I F H A E B G D
mystery = 2
```

---

## Part 6: Complexity Analysis

| Algorithm | Time | Space | Reason |
| --- | --- | --- | --- |
| Recursive pre/in/postorder | $O(n)$ | $O(h)$ | Each node visited once; stack holds one frame per level |
| Iterative pre/in/postorder | $O(n)$ | $O(h)$ ($O(n)$ for two-stack postorder) | Each node pushed and popped once |
| Level order | $O(n)$ | $O(w)$ | Queue holds at most one level (width $w$) |
| Height, count, leaves | $O(n)$ | $O(h)$ | One visit per node |
| Build from pre + in (linear search) | $O(n^2)$ | $O(n)$ | Searching inorder for each root |
| Build from pre + in (hash map) | $O(n)$ | $O(n)$ | Index lookup in $O(1)$ |

**Writing the recurrence** (often asked for 2–3 marks). For a traversal on a tree whose left subtree has $k$ nodes:
- Recurrence: $T(n) = T(k) + T(n - k - 1) + c$, with $T(0) = c$.
- Each node contributes a constant amount of work, so by induction $T(n) = O(n)$.
- Space is $O(h)$, and the height $h$ ranges from $\log_2 n$ (balanced) to $n - 1$ (skewed).
- So the space is between $O(\log n)$ and $O(n)$.

---

## Question Bank With Model Answers

All questions are **representative exam-style questions** (see the note at the top).

### Short questions (1–2 marks)

**Q9. Define a leaf node.** A node with no children (degree 0). *Exam tree: E, G, H, I.*

**Q10. What is the degree of a tree?** The maximum degree of any node in it. For a binary tree, at most 2.

**Q11. Define the depth of a node.** The number of edges on the path from the root to the node.

**Q12. What is the maximum number of nodes in a binary tree of height 4 (edges)?** $2^{5} - 1 = 31$.

**Q13. How many null pointers does a binary tree with 20 nodes have?** $n + 1 = 21$.

**Q14. A binary tree has 10 leaves. How many nodes have two children?** $n_2 = n_0 - 1 = 9$.

**Q15. Which traversal of an expression tree gives postfix notation?** Postorder.

**Q16. Which traversal is used to delete a tree, and why?** Postorder, because children must be deleted before their parent.

**Q17. What is a skewed binary tree?** A tree in which every internal node has only one child (all left, or all right); it behaves like a linked list.

**Q18. In 1-based array representation, where is the parent of the node at index 11?** $\lfloor 11/2 \rfloor = 5$.

**Q19. Can preorder and postorder together always identify a binary tree?** No; only for full binary trees (inorder is needed in general).

**Q20. What data structure is used for level-order traversal?** A queue.

### Medium questions (3–5 marks)

**Q21. Differentiate between a complete and a full binary tree with diagrams.**

- Use the comparison table from Q2 in Part 1.
- Draw the "full (not complete)" and "complete (not full)" trees from the types diagram.
- State that a perfect tree is both full and complete.

**Q22. Write the preorder, inorder and postorder of the exam tree.**

- Preorder: `A B D G E C F H I`
- Inorder: `D G B E A H F I C`
- Postorder: `G D E B H I F C A`

**Q23. Prove that $n_0 = n_2 + 1$.**

See the proof under "Properties of binary trees" in Part 1.

**Q24. Represent the exam tree using the array method and state one disadvantage.**

- Representation: see the array table in Part 1.
- Disadvantage: wasted slots. Here 13 slots hold 9 nodes, and for skewed trees the waste grows exponentially.

**Q25. Draw the expression tree for `(a + b) * (c - d)` and write its prefix and postfix forms.**

- Prefix: `* + a b - c d`
- Postfix: `a b + c d - *`

**Q26. Write a recursive function to count the leaf nodes of a binary tree and state its complexity.**

- Algorithm: `LEAVES(T)` in Part 4.
- Time: $O(n)$. Space: $O(h)$.

### Broad questions (5–10 marks)

**Q27. (a) Construct the binary tree from preorder `A B D E C F G` and inorder `D B E A F C G`. (b) Write its postorder.**

- (a) See Q4 in Part 3.
- (b) Postorder: **D E B F G C A**.

**Q28. Write a non-recursive algorithm for inorder traversal. Trace it on the exam tree, showing the stack after each visit.**

Algorithm: `ITERATIVE-INORDER` in Part 4. Trace (stack shown bottom → top **after** each visit):

| Visit | Stack after visit |
| --- | --- |
| D | A, B |
| G | A, B |
| B | A |
| E | A |
| A | (empty) |
| H | C, F |
| F | C |
| I | C |
| C | (empty) |

Output: `D G B E A H F I C`.

**Q29. Explain the linked and sequential representations of a binary tree. Compare them with respect to memory, insertion and parent lookup.**

See "Binary tree representations" in Part 1.

**Q30. Write recursive algorithms for preorder, inorder and postorder. Derive their time and space complexity.**

- Algorithms: Part 4.
- Complexity: Part 6.

### Output tracing questions

**Q31. Trace `inorder(1)` on `1(2(4, 5), 3)`.**

`4 2 5 1 3` (see the table for Q7 in Part 5).

**Q32. What are the outputs of `fun` and `mystery` on the exam tree?**

- `fun` prints `C I F H A E B G D`.
- `mystery` returns `2`.

**Q33. What does this print for the exam tree?**

```cpp nocompile
void g(Node* t, int depth) {
    if (!t) {
        return;
    }
    if (depth == 2) {
        cout << t->data << ' ';
    }
    g(t->left, depth + 1);
    g(t->right, depth + 1);
}
// called as g(root, 0)
```

It prints the nodes at depth 2 in left-to-right order: **D E F**.

### Diagram-based questions

**Q34. From the exam tree diagram, list: (a) siblings of D, (b) ancestors of I, (c) the subtree rooted at C, (d) the height of B.**

- (a) E
- (b) A, C, F
- (c) C, F, H, I
- (d) 2 (path B–D–G)

**Q35. Draw the binary tree with level-order array (1-based) `[1: P, 2: Q, 3: R, 4: –, 5: S, 6: T]` and write its inorder.**

- P has children Q (index 2) and R (index 3).
- Q has only a right child, S (index 5).
- R has a left child, T (index 6).
- Inorder: **Q S P T R**

**Q36. Is the exam tree height-balanced? Check every node.**

- At D: left height −1, right 0 ✓
- At B: left 1 (D–G), right 0 ✓
- At F: left 0, right 0 ✓
- At C: left 1, right −1 → difference 2 ✗

**Not balanced** (it fails at C).

### Algorithm-writing questions

**Q37. Write an algorithm to find the height of a binary tree.**

See `HEIGHT(T)` in Part 4.

**Q38. Write an algorithm to count internal nodes.**

Either compute `COUNT(T) − LEAVES(T)`, or count directly:

```text
INTERNAL(T)
1. If T = NULL, return 0.
2. If LEFT[T] = NULL and RIGHT[T] = NULL, return 0.   // a leaf
3. Return 1 + INTERNAL(LEFT[T]) + INTERNAL(RIGHT[T]).
```

**Q39. Write an algorithm for level-order traversal.**

Use a queue:

1. Enqueue the root.
2. While the queue is not empty, dequeue a node and visit it.
3. Enqueue its non-null children (left, then right).

See [level order](/posts/tree-binary-tree-traversals/#level-order-traversal-brief-introduction).

### Viva questions (short oral answers)

**Q40. Why is a tree called non-linear?** One node can be followed by several nodes; there is no single "next".

**Q41. Difference between a tree and a graph?** A tree is a connected graph with no cycles and exactly $n - 1$ edges; a graph can have cycles and be disconnected.

**Q42. What is the root's depth? A leaf's height?** Both 0 (edge convention).

**Q43. Why do we need the base case `if (root == NULL) { return; }`?** Without it the function dereferences a null pointer and never stops recursing.

**Q44. What is the time complexity of any traversal?** $O(n)$.

**Q45. Which traversal gives sorted output?** Inorder, on a binary search tree only.

**Q46. What is the space complexity of recursive traversal, and when is it worst?** $O(h)$; worst ($O(n)$) for a skewed tree.

**Q47. Why use a stack for iterative DFS traversals but a queue for level order?** DFS must return to the most recently postponed node (LIFO); level order processes nodes in the order they were discovered (FIFO).

**Q48. Can you build a tree from inorder alone?** No; many trees share the same inorder.

**Q49. What is a complete binary tree used for?** Binary heaps (stored in an array without gaps).

**Q50. What is a threaded binary tree?** A tree whose null right (or left) pointers point to the inorder successor (or predecessor), allowing traversal without a stack.

**Q51. What is the difference between a binary tree and a BST?** A BST adds the ordering rule left < root < right for whole subtrees. See the [BST exam guide](/posts/bst-questions-university-exams-bangladesh/).

**Q52. What happens in memory when you call `new Node('A')`?** A node is allocated on the heap and its address returned; it must be freed with `delete`.

**Q53. How many different binary trees have 3 nodes (unlabelled)?** 5 (the Catalan number $C_3$).

**Q54. What is a forest?** A set of disjoint trees; removing the root of a tree leaves a forest of its subtrees.

---

## Exam-Writing Tips

1. **Draw first, then answer.** Redraw the given tree neatly with left and right children clearly separated; most traversal mistakes come from a messy copy.
2. **State conventions** (height in edges or nodes; level starting at 0 or 1) in one line.
3. **Show the subtree split** when writing a traversal: "A, then preorder(B) = ..., then preorder(C) = ..." earns method marks even if you slip once.
4. **Verify reconstructed trees** by regenerating the given traversals.
5. **For algorithms,** write the base case first, then the recursive case, then complexity with a one-line reason.
6. **Lab exams:** always initialise `left` and `right` to `nullptr`, test with an empty tree and a one-node tree, and free memory in postorder.

## Next Steps

1. **Next:** [BST Questions for University Exams in Bangladesh](/posts/bst-questions-university-exams-bangladesh/): insertion sequences, deletion traces and BST algorithms.
2. Revise the concepts in [Trees, Binary Trees & Traversals](/posts/tree-binary-tree-traversals/) or practise coding with [Tree Interview Questions](/posts/tree-interview-questions/).
3. Graph traversal is usually the next exam chapter: [What Are DFS and BFS?](/posts/what-is-dfs-and-bfs/)
