Bird
Raised Fist0
DSA C#programming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Inorder 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 inorder traversal?
Given the binary tree below, what is the output of the inorder traversal (Left, Root, Right)?
DSA C#
class Node {
    public int val;
    public Node left, right;
    public Node(int v) { val = v; left = right = null; }
}

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

Node root = new Node(2);
root.left = new Node(1);
root.right = new Node(3);

void Inorder(Node node) {
    if (node == null) return;
    Inorder(node.left);
    Console.Write(node.val + " ");
    Inorder(node.right);
}

Inorder(root);
A3 2 1
B1 2 3
C2 1 3
D1 3 2
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree first, then root, then right subtree.
❓ Predict Output
intermediate
2:00remaining
Inorder traversal output for unbalanced tree
What is the output of the inorder traversal for this unbalanced tree?
DSA C#
class Node {
    public int val;
    public Node left, right;
    public Node(int v) { val = v; left = right = null; }
}

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

Node root = new Node(5);
root.left = new Node(3);
root.left.right = new Node(4);

void Inorder(Node node) {
    if (node == null) return;
    Inorder(node.left);
    Console.Write(node.val + " ");
    Inorder(node.right);
}

Inorder(root);
A3 5 4
B5 3 4
C4 3 5
D3 4 5
Attempts:
2 left
💡 Hint
Remember to visit left subtree fully before root, then right subtree.
🔧 Debug
advanced
2:00remaining
Identify the error in inorder traversal code
What error will this code produce when performing inorder traversal?
DSA C#
class Node {
    public int val;
    public Node left, right;
    public Node(int v) { val = v; left = right = null; }
}

void Inorder(Node node) {
    if (node == null) return;
    Inorder(node.right);
    Console.Write(node.val + " ");
    Inorder(node.left);
}

Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);

Inorder(root);
A3 1 2
B2 1 3
C1 3 2
DSyntaxError
Attempts:
2 left
💡 Hint
Check the order of recursive calls in the traversal function.
🧠 Conceptual
advanced
1:00remaining
How many nodes are visited in inorder traversal?
Given a binary tree with 7 nodes, how many nodes does the inorder traversal visit?
ADepends on the tree structure
B8
C7
D6
Attempts:
2 left
💡 Hint
Traversal visits every node exactly once.
🚀 Application
expert
3:00remaining
What is the inorder traversal output of this complex tree?
Given the tree below, what is the inorder traversal output?
DSA C#
class Node {
    public int val;
    public Node left, right;
    public Node(int v) { val = v; left = right = null; }
}

// Tree structure:
//       10
//      /  \
//     5    15
//    / \     \
//   3   7     20
//      /     /  \
//     6     17  25

Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.right = new Node(7);
root.left.right.left = new Node(6);
root.right.right = new Node(20);
root.right.right.left = new Node(17);
root.right.right.right = new Node(25);

void Inorder(Node node) {
    if (node == null) return;
    Inorder(node.left);
    Console.Write(node.val + " ");
    Inorder(node.right);
}

Inorder(root);
A3 5 6 7 10 15 17 20 25
B3 6 7 5 10 15 17 20 25
C10 5 3 7 6 15 20 17 25
D3 5 7 6 10 15 20 17 25
Attempts:
2 left
💡 Hint
Follow left subtree fully, then root, then right subtree recursively.

Practice

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

Solution

  1. Step 1: Understand inorder traversal definition

    Inorder traversal means visiting the left subtree first, then the root node, and finally the right subtree.
  2. Step 2: Apply the order to the tree nodes

    This order ensures nodes are visited in ascending order for binary search trees.
  3. Final Answer:

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

    Inorder = Left, Root, Right [OK]
Hint: Remember: Inorder = Left, Root, Right [OK]
Common Mistakes:
  • Confusing inorder with preorder or postorder
  • Visiting root before left subtree
  • Visiting right subtree before root
2. Which of the following C# method signatures correctly represents an inorder traversal method for a binary tree node class named Node?
easy
A. void InorderTraversal(Node root)
B. int InorderTraversal(Node root)
C. Node InorderTraversal()
D. bool InorderTraversal(Node root)

Solution

  1. Step 1: Identify the purpose of the method

    An inorder traversal method typically visits nodes and prints or processes them without returning a value, so it should be void.
  2. Step 2: Check method signature options

    void InorderTraversal(Node root) uses void return type and accepts a Node parameter, which is correct for traversal.
  3. Final Answer:

    void InorderTraversal(Node root) -> Option A
  4. Quick Check:

    Traversal methods usually return void [OK]
Hint: Traversal methods usually return void [OK]
Common Mistakes:
  • Using return types like int or bool unnecessarily
  • Missing the root parameter
  • Returning Node instead of void
3. Given the following binary tree:
    2
   / \
  1   3

What is the output of the inorder traversal method that prints node values?
medium
A. 1 3 2
B. 2 1 3
C. 3 2 1
D. 1 2 3

Solution

  1. Step 1: Apply inorder traversal on the tree

    Visit left subtree (1), then root (2), then right subtree (3).
  2. Step 2: Write the visited nodes in order

    The nodes are visited in order: 1, 2, 3.
  3. Final Answer:

    1 2 3 -> Option D
  4. Quick Check:

    Inorder traversal output = 1 2 3 [OK]
Hint: Inorder prints nodes in ascending order for BST [OK]
Common Mistakes:
  • Printing root before left subtree
  • Swapping left and right subtree order
  • Confusing with preorder or postorder output
4. Consider this C# inorder traversal method:
void InorderTraversal(Node root) {
    if (root == null) return;
    InorderTraversal(root.Right);
    Console.Write(root.Value + " ");
    InorderTraversal(root.Left);
}

What is the error in this code?
medium
A. It causes infinite recursion
B. It misses the base case for null root
C. It visits right subtree before left subtree, reversing inorder order
D. It does not print the root value

Solution

  1. Step 1: Analyze the traversal order in the code

    The code visits root.Right first, then root, then root.Left, which is reverse of inorder.
  2. Step 2: Identify the correct inorder order

    Inorder should visit left subtree first, then root, then right subtree.
  3. Final Answer:

    It visits right subtree before left subtree, reversing inorder order -> Option C
  4. Quick Check:

    Inorder = Left, Root, Right [OK]
Hint: Inorder must visit left subtree before right [OK]
Common Mistakes:
  • Swapping left and right subtree calls
  • Forgetting to check for null root
  • Not printing root value
5. You have a binary tree where each node contains an integer. You want to collect all node values in a list using inorder traversal. Which C# code snippet correctly implements this?
hard
A. void Inorder(Node root, List<int> result) { if (root == null) return; result.Add(root.Value); Inorder(root.Left, result); Inorder(root.Right, result); }
B. void Inorder(Node root, List<int> result) { if (root == null) return; Inorder(root.Left, result); result.Add(root.Value); Inorder(root.Right, result); }
C. void Inorder(Node root, List<int> result) { if (root == null) return; Inorder(root.Right, result); result.Add(root.Value); Inorder(root.Left, result); }
D. void Inorder(Node root, List<int> result) { if (root == null) return; Inorder(root.Left, result); Inorder(root.Right, result); result.Add(root.Value); }

Solution

  1. Step 1: Recall inorder traversal order

    Inorder visits left subtree, then root, then right subtree.
  2. Step 2: Check which code matches this order

    void Inorder(Node root, List<int> result) { if (root == null) return; Inorder(root.Left, result); result.Add(root.Value); Inorder(root.Right, result); } visits left subtree, adds root value, then visits right subtree, correctly collecting values in order.
  3. Final Answer:

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

    Left, Root, Right order with result.Add(root.Value) in middle [OK]
Hint: Add root value between left and right recursive calls [OK]
Common Mistakes:
  • Adding root value before left subtree
  • Adding root value after right subtree
  • Swapping left and right subtree calls
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