Skip to content
Shafin Chowdhury
Go back

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

Software Developer & Computer Science Student

Published:
20 min read
Edit page
Table of Contents23 topics

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; this article is the exam-practice companion. Next: BST Questions for University Exams.


The Exam Tree Used Throughout


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≥0n \ge 0 disjoint sets T1,T2,…,TnT_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 nn nodes has n−1n - 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)

TermDefinitionFrom the exam tree
RootNode with no parentA
Leaf / terminal nodeNode with no childrenE, G, H, I
Internal / non-terminal nodeNode with at least one childA, B, C, D, F
Parent, childDirect predecessor, direct successorF is the parent of H and I
SiblingsChildren of the same parentH and I; B and C
Ancestors of a nodeAll nodes on the path from the root to itAncestors of H: A, C, F
DescendantsAll nodes in its subtree except itselfDescendants of B: D, E, G
Degree of a nodeNumber of childrendeg(A) = 2, deg(C) = 1, deg(E) = 0
Depth / level of a nodeEdges from the rootdepth(G) = 3
Height of the treeLongest root-to-leaf path in edges3 (A–B–D–G or A–C–F–H)
Path lengthNumber of edges in the pathA to I: 3

Q2. Differentiate full, complete and perfect binary trees

FeatureFull (strict)CompletePerfect
RuleEvery node has 0 or 2 childrenAll levels full except possibly the last; last level filled left to rightAll internal nodes have 2 children and all leaves at the same level
Can a node have 1 child?NoYes (at most one such node, on the second-last level)No
Nodes for height hhbetween 2h+12h + 1 and 2h+1−12^{h+1} - 1between 2h2^h and 2h+1−12^{h+1} - 1exactly 2h+1−12^{h+1} - 1
Relationship——Every perfect tree is both full and complete

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 hh in edges:

  1. The maximum number of nodes on level ii is 2i2^i.
  2. The maximum number of nodes in a binary tree of height hh is 2h+1−12^{h+1} - 1.
  3. For any non-empty binary tree, n0=n2+1n_0 = n_2 + 1, where n0n_0 = number of leaves and n2n_2 = number of nodes with two children.
    • Check on the exam tree: leaves E, G, H, I give n0=4n_0 = 4.
    • Nodes with two children A, B, F give n2=3n_2 = 3. ✓
  4. A binary tree with nn nodes has exactly n−1n - 1 edges and n+1n + 1 null links.
  5. The minimum height of a binary tree with nn nodes is ⌈log⁡2(n+1)⌉−1\lceil \log_2(n+1) \rceil - 1.

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

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

Binary tree representations

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

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 kk:

  • left child at 2k2k
  • right child at 2k+12k + 1
  • parent at ⌊k/2⌋\lfloor k/2 \rfloor

(With 0-based indexing: 2k+12k + 1, 2k+22k + 2 and ⌊(k−1)/2⌋\lfloor (k-1)/2 \rfloor.)

The exam tree in a 1-based array:

Index12345678910111213
ValueABCDEF––G––HI
  • G is the right child of D (index 4), so it sits at 2⋅4+1=92 \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.
LinkedArray
MemoryTwo pointers per nodeCan waste many empty slots
Insert / deleteEasyHard (may need resizing)
Find parentNeeds an extra pointer⌊k/2⌋\lfloor k/2 \rfloor, O(1)O(1)
Best forGeneral treesComplete 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.
A B C D E F G H I

Output
Step through the exam tree's traversals to check your hand-written answers.

Expression trees (a favourite 3–5 mark question)

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:

  • 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.

  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.

  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

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.”

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

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.

#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:

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.

#Call / actionCall stack after (bottom → top)Output so far
1inorder(1) → goes left1
2inorder(2) → goes left1, 2
3inorder(4); left is NULL; print 4; right is NULL; return1, 24
4back in 2: print 2; go right1, 24 2
5inorder(5): print 5; return1, 24 2 5
62 returns; back in 1: print 1; go right14 2 5 1
7inorder(3): print 3; return14 2 5 1 3
81 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)O(h).

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

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:

#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:

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

Part 6: Complexity Analysis

AlgorithmTimeSpaceReason
Recursive pre/in/postorderO(n)O(n)O(h)O(h)Each node visited once; stack holds one frame per level
Iterative pre/in/postorderO(n)O(n)O(h)O(h) (O(n)O(n) for two-stack postorder)Each node pushed and popped once
Level orderO(n)O(n)O(w)O(w)Queue holds at most one level (width ww)
Height, count, leavesO(n)O(n)O(h)O(h)One visit per node
Build from pre + in (linear search)O(n2)O(n^2)O(n)O(n)Searching inorder for each root
Build from pre + in (hash map)O(n)O(n)O(n)O(n)Index lookup in O(1)O(1)

Writing the recurrence (often asked for 2–3 marks). For a traversal on a tree whose left subtree has kk nodes:

  • Recurrence: T(n)=T(k)+T(n−k−1)+cT(n) = T(k) + T(n - k - 1) + c, with T(0)=cT(0) = c.
  • Each node contributes a constant amount of work, so by induction T(n)=O(n)T(n) = O(n).
  • Space is O(h)O(h), and the height hh ranges from log⁡2n\log_2 n (balanced) to n−1n - 1 (skewed).
  • So the space is between O(log⁡n)O(\log n) and O(n)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)? 25−1=312^{5} - 1 = 31.

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

Q14. A binary tree has 10 leaves. How many nodes have two children? n2=n0−1=9n_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? ⌊11/2⌋=5\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 n0=n2+1n_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)O(n). Space: O(h)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):

VisitStack after visit
DA, B
GA, B
BA
EA
A(empty)
HC, F
FC
IC
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?

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:

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.

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−1n - 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)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)O(h); worst (O(n)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.

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 C3C_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: insertion sequences, deletion traces and BST algorithms.
  2. Revise the concepts in Trees, Binary Trees & Traversals or practise coding with Tree Interview Questions.
  3. Graph traversal is usually the next exam chapter: What Are DFS and BFS?

Edit page
Share this post:

Related Articles

Continue exploring related topics and foundational concepts.


Previous Post
Binary Search Tree (BST) Questions for University Exams in Bangladesh: Construction, Deletion & Viva (With Answers)
Next Post
Most Common Binary Search Tree (BST) Interview Questions (With C++ Solutions)