Bird
Raised Fist0
DSA C#programming~20 mins

Create a Binary Tree Manually in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Binary Tree 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 manual binary tree creation?
Consider the following C# code that creates a binary tree manually. What will be printed when we traverse the tree in preorder (root, left, right)?
DSA C#
public class Node {
    public int Value;
    public Node Left, Right;
    public Node(int value) {
        Value = value;
        Left = Right = null;
    }
}

public class BinaryTree {
    public Node Root;

    public void Preorder(Node node) {
        if (node == null) return;
        System.Console.Write(node.Value + " ");
        Preorder(node.Left);
        Preorder(node.Right);
    }
}

var tree = new BinaryTree();
tree.Root = new Node(10);
tree.Root.Left = new Node(5);
tree.Root.Right = new Node(15);
tree.Root.Left.Left = new Node(3);
tree.Root.Left.Right = new Node(7);
tree.Root.Right.Right = new Node(18);
tree.Preorder(tree.Root);
A10 5 3 7 15 18
B10 15 18 5 7 3
C3 5 7 10 15 18
D5 3 7 10 15 18
Attempts:
2 left
💡 Hint
Preorder traversal visits the root node first, then the left subtree, then the right subtree.
❓ Predict Output
intermediate
2:00remaining
What is the output after inserting nodes manually?
Given the following code that manually creates a binary tree, what will be the output of the inorder traversal (left, root, right)?
DSA C#
public class Node {
    public int Value;
    public Node Left, Right;
    public Node(int value) {
        Value = value;
        Left = Right = null;
    }
}

public class BinaryTree {
    public Node Root;

    public void Inorder(Node node) {
        if (node == null) return;
        Inorder(node.Left);
        System.Console.Write(node.Value + " ");
        Inorder(node.Right);
    }
}

var tree = new BinaryTree();
tree.Root = new Node(8);
tree.Root.Left = new Node(3);
tree.Root.Right = new Node(10);
tree.Root.Left.Left = new Node(1);
tree.Root.Left.Right = new Node(6);
tree.Root.Right.Right = new Node(14);
tree.Inorder(tree.Root);
A1 6 3 8 10 14
B8 3 1 6 10 14
C3 1 6 8 14 10
D1 3 6 8 10 14
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree first, then root, then right subtree.
🔧 Debug
advanced
2:00remaining
Why does this manual binary tree creation cause a NullReferenceException?
Examine the following code that tries to create a binary tree manually. Why does it cause a NullReferenceException when calling Preorder traversal?
DSA C#
public class Node {
    public int Value;
    public Node Left, Right;
    public Node(int value) {
        Value = value;
    }
}

public class BinaryTree {
    public Node Root;

    public void Preorder(Node node) {
        if (node == null) return;
        System.Console.Write(node.Value + " ");
        Preorder(node.Left);
        Preorder(node.Right);
    }
}

var tree = new BinaryTree();
tree.Root = new Node(20);
tree.Root.Left.Value = 10;
tree.Root.Right = new Node(30);
tree.Preorder(tree.Root);
ABecause tree.Root.Left is null and we try to access Value property on null
BBecause Node constructor does not initialize Value
CBecause Preorder method is missing a base case
DBecause tree.Root.Right is not initialized
Attempts:
2 left
💡 Hint
Check if all child nodes are initialized before accessing their properties.
🧠 Conceptual
advanced
1:30remaining
How many nodes are in this manually created binary tree?
Given the following manual binary tree creation code, how many nodes does the tree contain?
DSA C#
public class Node {
    public int Value;
    public Node Left, Right;
    public Node(int value) {
        Value = value;
        Left = Right = null;
    }
}

var root = new Node(1);
root.Left = new Node(2);
root.Right = new Node(3);
root.Left.Left = new Node(4);
root.Left.Right = new Node(5);
root.Right.Left = new Node(6);
root.Right.Right = new Node(7);
root.Left.Left.Left = new Node(8);
A9
B7
C8
D6
Attempts:
2 left
💡 Hint
Count each node created with new Node(...)
🚀 Application
expert
2:30remaining
What is the output of this manual binary tree level order traversal?
Given the following code that manually creates a binary tree and performs level order traversal (breadth-first), what will be printed?
DSA C#
using System.Collections.Generic;

public class Node {
    public int Value;
    public Node Left, Right;
    public Node(int value) {
        Value = value;
        Left = Right = null;
    }
}

public class BinaryTree {
    public Node Root;

    public void LevelOrder() {
        if (Root == null) return;
        Queue<Node> queue = new Queue<Node>();
        queue.Enqueue(Root);
        while (queue.Count > 0) {
            Node current = queue.Dequeue();
            System.Console.Write(current.Value + " ");
            if (current.Left != null) queue.Enqueue(current.Left);
            if (current.Right != null) queue.Enqueue(current.Right);
        }
    }
}

var tree = new BinaryTree();
tree.Root = new Node(1);
tree.Root.Left = new Node(2);
tree.Root.Right = new Node(3);
tree.Root.Left.Left = new Node(4);
tree.Root.Left.Right = new Node(5);
tree.Root.Right.Right = new Node(6);
tree.LevelOrder();
A1 3 2 6 5 4
B1 2 3 4 5 6
C4 5 6 2 3 1
D2 3 1 4 5 6
Attempts:
2 left
💡 Hint
Level order traversal visits nodes level by level from left to right.

Practice

(1/5)
1. What is the correct way to create a root node manually in a binary tree using C#?
easy
A. Node root = new Node(); root = 10;
B. Node root = Node(10);
C. Node root = new Node(); root.value = 10;
D. Node root = new Node(10);

Solution

  1. Step 1: Understand Node creation syntax

    In C#, to create an object, use the 'new' keyword followed by the constructor call with parameters.
  2. Step 2: Apply correct syntax for root node

    Using 'new Node(10)' correctly creates a node with value 10 assigned.
  3. Final Answer:

    Node root = new Node(10); -> Option D
  4. Quick Check:

    Use 'new' keyword with constructor to create node [OK]
Hint: Use 'new' keyword with constructor to create nodes [OK]
Common Mistakes:
  • Omitting 'new' keyword when creating node
  • Assigning value directly to node variable
  • Calling constructor without 'new'
2. Which of the following is the correct syntax to assign a left child node with value 5 to a root node named root?
easy
A. root.left = new Node(5);
B. root.left = Node(5);
C. root.left.value = 5;
D. root.left = 5;

Solution

  1. Step 1: Understand child node assignment

    Left child must be assigned a new Node object, not just a value.
  2. Step 2: Use correct syntax to assign left child

    Assigning 'root.left = new Node(5);' creates a new node with value 5 as left child.
  3. Final Answer:

    root.left = new Node(5); -> Option A
  4. Quick Check:

    Assign new Node object to left child [OK]
Hint: Assign new Node object, not just value, to child [OK]
Common Mistakes:
  • Assigning value directly instead of Node object
  • Omitting 'new' keyword
  • Trying to assign value to left property directly
3. Given the following code, what is the output of an inorder traversal (left, root, right)?
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
medium
A. [10, 5, 3, 15]
B. [3, 5, 10, 15]
C. [5, 3, 10, 15]
D. [3, 10, 5, 15]

Solution

  1. Step 1: Understand inorder traversal order

    Inorder visits left subtree, then root, then right subtree.
  2. Step 2: Trace traversal on given tree

    Left subtree of root (10) is node 5, which has left child 3. So traversal order is 3, 5, 10, 15.
  3. Final Answer:

    [3, 5, 10, 15] -> Option B
  4. Quick Check:

    Inorder = left, root, right [OK]
Hint: Inorder = left subtree, root, then right subtree [OK]
Common Mistakes:
  • Confusing preorder or postorder with inorder
  • Missing leftmost node in traversal
  • Listing nodes in insertion order
4. Identify the error in the following code that tries to create a binary tree:
Node root = new Node(20);
root.left = 10;
root.right = new Node(30);
medium
A. Right child node value is incorrect
B. Missing 'new' keyword when creating root node
C. Assigning integer directly to 'left' instead of a Node object
D. Root node value should be a string, not integer

Solution

  1. Step 1: Check left child assignment

    Left child must be a Node object, but code assigns integer 10 directly.
  2. Step 2: Verify other parts

    Root node and right child are correctly created with 'new Node()'.
  3. Final Answer:

    Assigning integer directly to 'left' instead of a Node object -> Option C
  4. Quick Check:

    Child nodes must be Node objects [OK]
Hint: Child nodes must be Node objects, not raw values [OK]
Common Mistakes:
  • Assigning raw values instead of Node objects
  • Forgetting 'new' keyword for node creation
  • Confusing node value type requirements
5. You want to manually create this binary tree:
      8
     / \
    3   10
   / \    \
  1   6    14
     / \   /
    4   7 13

Which code snippet correctly creates this tree?
hard
A. Node root = new Node(8); root.left = new Node(3); root.right = new Node(10); root.left.left = new Node(1); root.left.right = new Node(6); root.left.right.left = new Node(4); root.left.right.right = new Node(7); root.right.right = new Node(14); root.right.right.left = new Node(13);
B. Node root = new Node(8); root.left = new Node(3); root.right = new Node(10); root.left.left = new Node(1); root.left.right = new Node(6); root.left.right.left = new Node(7); root.left.right.right = new Node(4); root.right.right = new Node(14); root.right.right.left = new Node(13);
C. Node root = new Node(8); root.left = new Node(3); root.right = new Node(10); root.left.left = new Node(1); root.left.right = new Node(6); root.left.right.left = new Node(4); root.left.right.right = new Node(7); root.right.left = new Node(14); root.right.left.left = new Node(13);
D. Node root = new Node(8); root.left = new Node(3); root.right = new Node(10); root.left.left = new Node(1); root.left.right = new Node(6); root.left.right.left = new Node(4); root.left.right.right = new Node(7); root.right.right = new Node(13); root.right.right.left = new Node(14);

Solution

  1. Step 1: Map tree structure to code

    Root 8 has left 3 and right 10. Node 3 has children 1 and 6. Node 6 has children 4 and 7. Node 10 has right child 14, which has left child 13.
  2. Step 2: Verify code matches tree

    Node root = new Node(8); root.left = new Node(3); root.right = new Node(10); root.left.left = new Node(1); root.left.right = new Node(6); root.left.right.left = new Node(4); root.left.right.right = new Node(7); root.right.right = new Node(14); root.right.right.left = new Node(13); correctly assigns all nodes to their proper left or right children matching the tree diagram.
  3. Final Answer:

    Option A code snippet correctly creates the tree -> Option A
  4. Quick Check:

    Match tree structure exactly in code [OK]
Hint: Match each node's left/right child exactly as tree diagram [OK]
Common Mistakes:
  • Swapping left and right children
  • Assigning children to wrong parent nodes
  • Mixing up node values in assignments
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