Bird
Raised Fist0
DSA C#programming~20 mins

Tree Traversal Postorder Left Right Root in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Postorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this postorder traversal?
Given the binary tree below, what is the output of the postorder traversal (Left, Right, Root)?
DSA C#
class Node {
    public int Value;
    public Node Left, Right;
    public Node(int val) { Value = val; Left = Right = null; }
}

// Tree structure:
//       1
//      / \
//     2   3
//    / \
//   4   5

void Postorder(Node root) {
    if (root == null) return;
    Postorder(root.Left);
    Postorder(root.Right);
    Console.Write(root.Value + " ");
}

// Call Postorder(root) where root is node with value 1
A1 2 3 4 5
B4 2 5 3 1
C2 4 5 3 1
D4 5 2 3 1
Attempts:
2 left
💡 Hint

Remember postorder visits left subtree, then right subtree, then the root node.

❓ Predict Output
intermediate
2:00remaining
What is the postorder traversal output of this tree?
Consider the binary tree: // 10 // / \ // 20 30 // / \ // 40 50 What is the output of postorder traversal (Left, Right, Root)?
DSA C#
class Node {
    public int Value;
    public Node Left, Right;
    public Node(int val) { Value = val; Left = Right = null; }
}

void Postorder(Node root) {
    if (root == null) return;
    Postorder(root.Left);
    Postorder(root.Right);
    Console.Write(root.Value + " ");
}

// Call Postorder(root) where root is node with value 10
A10 20 30 40 50
B20 40 50 30 10
C20 30 40 50 10
D40 50 30 20 10
Attempts:
2 left
💡 Hint

Postorder visits left subtree first, then right subtree, then root.

🔧 Debug
advanced
2:00remaining
Why does this postorder traversal code cause a runtime error?
Examine the following code snippet for postorder traversal. Why does it cause a runtime error when run on a non-empty tree?
DSA C#
class Node {
    public int Value;
    public Node Left, Right;
    public Node(int val) { Value = val; Left = Right = null; }
}

void Postorder(Node root) {
    Postorder(root.Left);
    Postorder(root.Right);
    Console.Write(root.Value + " ");
}

// Called with a valid root node
AIt causes a NullReferenceException because root.Value is accessed before checking if root is null.
BIt causes no error and prints the postorder traversal correctly.
CIt causes a compile-time error because the method is missing a return type.
DIt causes a StackOverflowException because there is no base case to stop recursion.
Attempts:
2 left
💡 Hint

Check if the code handles the case when root is null before accessing its members.

🧠 Conceptual
advanced
1:00remaining
Which traversal order matches postorder traversal?
Select the correct order of visiting nodes in postorder traversal of a binary tree.
ALeft, Right, Root
BLeft, Root, Right
CRoot, Left, Right
DRight, Left, Root
Attempts:
2 left
💡 Hint

Postorder means visiting children before the parent.

🚀 Application
expert
3:00remaining
What is the postorder traversal output after inserting nodes in this order?
You insert nodes into a binary search tree in this order: 15, 10, 20, 8, 12, 17, 25. What is the postorder traversal output of the resulting tree?
DSA C#
class Node {
    public int Value;
    public Node Left, Right;
    public Node(int val) { Value = val; Left = Right = null; }
}

Node Insert(Node root, int val) {
    if (root == null) return new Node(val);
    if (val < root.Value) root.Left = Insert(root.Left, val);
    else root.Right = Insert(root.Right, val);
    return root;
}

void Postorder(Node root) {
    if (root == null) return;
    Postorder(root.Left);
    Postorder(root.Right);
    Console.Write(root.Value + " ");
}

// Build tree by inserting 15,10,20,8,12,17,25
// Then call Postorder(root)
A10 8 12 25 17 20 15
B15 10 8 12 20 17 25
C8 12 10 17 25 20 15
D8 10 12 17 20 25 15
Attempts:
2 left
💡 Hint

Build the BST step by step, then do postorder traversal (Left, Right, Root).

Practice

(1/5)
1. What is the order of nodes visited in a postorder tree traversal?
easy
A. Left subtree, Right subtree, Root node
B. Root node, Left subtree, Right subtree
C. Root node, Right subtree, Left subtree
D. Right subtree, Left subtree, Root node

Solution

  1. Step 1: Understand postorder traversal definition

    Postorder traversal visits the left subtree first, then the right subtree, and finally the root node.
  2. Step 2: Confirm the visiting order

    This order ensures children nodes are processed before their parent node.
  3. Final Answer:

    Left subtree, Right subtree, Root node -> Option A
  4. Quick Check:

    Postorder = Left, Right, Root [OK]
Hint: Remember: postorder ends with the root node [OK]
Common Mistakes:
  • Confusing postorder with preorder or inorder
  • Visiting root node first instead of last
  • Swapping left and right subtree order
2. Which of the following C# method signatures correctly represents a postorder traversal of a binary tree node named root?
easy
A. void PostorderTraversal(Node root) { PostorderTraversal(root.Right); PostorderTraversal(root.Left); Console.WriteLine(root.Value); }
B. void PostorderTraversal(Node root) { Console.WriteLine(root.Value); PostorderTraversal(root.Left); PostorderTraversal(root.Right); }
C. void PostorderTraversal(Node root) { PostorderTraversal(root.Left); PostorderTraversal(root.Right); Console.WriteLine(root.Value); }
D. void PostorderTraversal(Node root) { Console.WriteLine(root.Value); PostorderTraversal(root.Right); PostorderTraversal(root.Left); }

Solution

  1. Step 1: Identify correct postorder call sequence

    Postorder traversal calls left subtree, then right subtree, then processes the root.
  2. Step 2: Match method calls to postorder order

    void PostorderTraversal(Node root) { PostorderTraversal(root.Left); PostorderTraversal(root.Right); Console.WriteLine(root.Value); } calls left, then right, then prints root value, matching postorder traversal.
  3. Final Answer:

    void PostorderTraversal(Node root) { PostorderTraversal(root.Left); PostorderTraversal(root.Right); Console.WriteLine(root.Value); } -> Option C
  4. Quick Check:

    Left, Right, Root call order = void PostorderTraversal(Node root) { PostorderTraversal(root.Left); PostorderTraversal(root.Right); Console.WriteLine(root.Value); } [OK]
Hint: Postorder prints after left and right recursive calls [OK]
Common Mistakes:
  • Printing root value before recursive calls
  • Swapping left and right subtree calls
  • Missing recursive calls for subtrees
3. Given the binary tree below, what is the output of a postorder traversal?

    1
   / \
  2   3
 / \
4   5
medium
A. [1, 2, 3, 4, 5]
B. [4, 5, 2, 3, 1]
C. [2, 4, 5, 3, 1]
D. [4, 2, 5, 3, 1]

Solution

  1. Step 1: Traverse left subtree of root (1)

    Left subtree is rooted at 2, with children 4 and 5. Postorder visits 4, then 5, then 2.
  2. Step 2: Traverse right subtree and root

    Right subtree is 3 (leaf), visited next. Finally, root 1 is visited last.
  3. Final Answer:

    [4, 5, 2, 3, 1] -> Option B
  4. Quick Check:

    Postorder = Left, Right, Root = [4,5,2,3,1] [OK]
Hint: Visit children fully before parent in postorder [OK]
Common Mistakes:
  • Visiting root before children
  • Swapping left and right subtree order
  • Confusing preorder or inorder output
4. The following C# code attempts a postorder traversal but produces incorrect output. What is the error?

void Postorder(Node root) {
  if (root == null) return;
  Console.WriteLine(root.Value);
  Postorder(root.Left);
  Postorder(root.Right);
}
medium
A. The root value is printed before traversing subtrees
B. The base case for null root is missing
C. Left and right subtree calls are swapped
D. The method does not return any value

Solution

  1. Step 1: Analyze the order of operations in code

    The code prints root value first, then traverses left and right subtrees.
  2. Step 2: Compare with postorder traversal rules

    Postorder requires visiting left and right subtrees before printing root value.
  3. Final Answer:

    Root value printed before subtrees traversal -> Option A
  4. Quick Check:

    Postorder prints after children, not before [OK]
Hint: Print root after recursive calls for postorder [OK]
Common Mistakes:
  • Printing root before recursive calls
  • Ignoring null base case (though present here)
  • Swapping left and right subtree calls
5. You want to collect all node values of a binary tree in postorder traversal into a list in C#. Which code snippet correctly implements this?
hard
A. void Postorder(Node root, List<int> result) { if(root == null) return; result.Add(root.Value); Postorder(root.Right, result); Postorder(root.Left, result); }
B. void Postorder(Node root, List<int> result) { if(root == null) return; result.Add(root.Value); Postorder(root.Left, result); Postorder(root.Right, result); }
C. void Postorder(Node root, List<int> result) { if(root == null) return; Postorder(root.Right, result); Postorder(root.Left, result); result.Add(root.Value); }
D. void Postorder(Node root, List<int> result) { if(root == null) return; Postorder(root.Left, result); Postorder(root.Right, result); result.Add(root.Value); }

Solution

  1. Step 1: Confirm traversal order in code

    void Postorder(Node root, List<int> result) { if(root == null) return; Postorder(root.Left, result); Postorder(root.Right, result); result.Add(root.Value); } calls left subtree, then right subtree, then adds root value to list, matching postorder.
  2. Step 2: Verify list collection logic

    Adding root value after recursive calls ensures correct postorder sequence in the list.
  3. Final Answer:

    void Postorder(Node root, List<int> result) { if(root == null) return; Postorder(root.Left, result); Postorder(root.Right, result); result.Add(root.Value); } -> Option D
  4. Quick Check:

    Left, Right, Root with Add after recursion [OK]
Hint: Add node value after left and right recursive calls [OK]
Common Mistakes:
  • Adding root value before recursive calls
  • Swapping left and right subtree calls
  • Not handling null root properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard