Bird
Raised Fist0
DSA Javaprogramming~20 mins

Create a Binary Tree Manually in DSA Java - 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
Output of Manual Binary Tree Creation
What is the printed output of the binary tree after manually creating nodes and linking them as shown in the code?
DSA Java
class Node {
    int value;
    Node left, right;
    Node(int value) {
        this.value = value;
        left = right = null;
    }
}

public class BinaryTree {
    public static void main(String[] args) {
        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.right.right = new Node(18);

        printInOrder(root);
    }

    static void printInOrder(Node node) {
        if (node == null) return;
        printInOrder(node.left);
        System.out.print(node.value + " -> ");
        printInOrder(node.right);
    }
}
A5 -> 3 -> 7 -> 10 -> 15 -> 18 ->
B10 -> 5 -> 3 -> 7 -> 15 -> 18 ->
C3 -> 7 -> 5 -> 10 -> 15 -> 18 ->
D3 -> 5 -> 7 -> 10 -> 15 -> 18 ->
Attempts:
2 left
💡 Hint
In-order traversal prints left subtree, then node, then right subtree.
❓ Predict Output
intermediate
2:00remaining
Output after Adding a New Node
After adding a new node with value 6 as the left child of node 7, what is the in-order traversal output?
DSA Java
class Node {
    int value;
    Node left, right;
    Node(int value) {
        this.value = value;
        left = right = null;
    }
}

public class BinaryTree {
    public static void main(String[] args) {
        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(18);

        printInOrder(root);
    }

    static void printInOrder(Node node) {
        if (node == null) return;
        printInOrder(node.left);
        System.out.print(node.value + " -> ");
        printInOrder(node.right);
    }
}
A3 -> 5 -> 6 -> 7 -> 10 -> 15 -> 18 ->
B3 -> 6 -> 7 -> 5 -> 10 -> 15 -> 18 ->
C3 -> 5 -> 7 -> 6 -> 10 -> 15 -> 18 ->
D5 -> 3 -> 6 -> 7 -> 10 -> 15 -> 18 ->
Attempts:
2 left
💡 Hint
In-order traversal visits left subtree, node, then right subtree recursively.
🔧 Debug
advanced
2:00remaining
Identify the Error in Binary Tree Node Linking
What error will occur when running this code that manually creates a binary tree?
DSA Java
class Node {
    int value;
    Node left, right;
    Node(int value) {
        this.value = value;
        left = right = null;
    }
}

public class BinaryTree {
    public static void main(String[] args) {
        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.right.left.right = new Node(18);

        printInOrder(root);
    }

    static void printInOrder(Node node) {
        if (node == null) return;
        printInOrder(node.left);
        System.out.print(node.value + " -> ");
        printInOrder(node.right);
    }
}
ANo error, prints: 3 -> 5 -> 7 -> 10 -> 15 -> 18 ->
BNullPointerException at root.right.left.right = new Node(18);
CSyntaxError due to missing semicolon
DArrayIndexOutOfBoundsException
Attempts:
2 left
💡 Hint
Check if all intermediate nodes are initialized before accessing their children.
🧠 Conceptual
advanced
1:30remaining
Number of Nodes in a Manually Created Binary Tree
Given the following manual binary tree creation code, how many nodes does the tree contain?
DSA Java
Node 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);
A6
B7
C8
D9
Attempts:
2 left
💡 Hint
Count each new Node() call in the code.
🚀 Application
expert
2:30remaining
Manual Binary Tree Height Calculation
What is the height of the binary tree created by the following code? Height is the number of edges on the longest path from root to a leaf.
DSA Java
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.left.left = new Node(1);
root.right.right = new Node(18);
root.right.right.right = new Node(20);
A3
B4
C5
D2
Attempts:
2 left
💡 Hint
Count edges from root to the deepest leaf node.

Practice

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

Solution

  1. Step 1: Understand Node object creation

    In Java, to create an object, you use the new keyword followed by the constructor call.
  2. Step 2: Apply correct syntax for constructor with value

    The constructor Node(10) creates a node with value 10. So new Node(10) is correct.
  3. Final Answer:

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

    Object creation with constructor = new Node(10) [OK]
Hint: Use 'new' keyword with constructor to create objects [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Using incorrect constructor syntax
  • Trying to assign value without constructor
2. Which of the following is the correct syntax to manually link a left child node with value 5 to a root node in Java?
easy
A. root.left = new Node(5);
B. root.leftNode = new Node(5);
C. root.left = Node(5);
D. root.left = new Node.left(5);

Solution

  1. Step 1: Understand node linking in binary tree

    The left child of a node is usually accessed by left field. Assigning a new node requires new Node(value).
  2. Step 2: Apply correct field and constructor syntax

    So root.left = new Node(5); correctly creates and links the left child.
  3. Final Answer:

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

    Link left child with new Node = root.left = new Node(5) [OK]
Hint: Use 'root.left = new Node(value)' to link left child [OK]
Common Mistakes:
  • Using wrong field name like leftNode
  • Omitting 'new' keyword
  • Incorrect constructor call without 'new'
3. Given the following code, what is the output of an inorder traversal (left, root, right)?
class Node {
  int value;
  Node left, right;
  Node(int v) { value = v; }
}

Node 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);

void inorder(Node node) {
  if (node == null) return;
  inorder(node.left);
  System.out.print(node.value + " ");
  inorder(node.right);
}

inorder(root);
medium
A. 4 2 5 1 3
B. 1 2 3 4 5
C. 1 3 2 4 5
D. 2 4 5 1 3

Solution

  1. Step 1: Understand inorder traversal order

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

    Left subtree of root(1) is node(2) with children 4 and 5.
    Traverse left subtree of 2: 4 (no children)
    Visit 2
    Traverse right subtree of 2: 5 (no children)
    Visit root 1
    Traverse right subtree of root: 3 (no children)
    Sequence: 4 2 5 1 3
  3. Final Answer:

    4 2 5 1 3 -> Option A
  4. Quick Check:

    Inorder = left, root, right = 4 2 5 1 3 [OK]
Hint: Inorder = left subtree, root, then right subtree [OK]
Common Mistakes:
  • Confusing preorder or postorder with inorder
  • Missing left or right subtree nodes
  • Printing root before left subtree
4. Identify the error in the following code that tries to create a binary tree manually:
class Node {
  int value;
  Node left, right;
  Node(int v) { value = v; }
}

Node root = new Node(10);
root.left = Node(5);
root.right = new Node(15);
medium
A. Node class constructor is missing
B. Cannot assign right child after root creation
C. Missing 'new' keyword when creating left child node
D. Value type should be String, not int

Solution

  1. Step 1: Check node creation syntax

    Creating a new node requires the 'new' keyword before constructor call.
  2. Step 2: Identify incorrect line

    Line root.left = Node(5); misses 'new' keyword, causing syntax error.
  3. Final Answer:

    Missing 'new' keyword when creating left child node -> Option C
  4. Quick Check:

    Object creation needs 'new' keyword [OK]
Hint: Always use 'new' to create objects in Java [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Confusing constructor call with method call
  • Assuming automatic object creation
5. You want to manually create a binary tree with root value 7, left child 3, right child 9, and left child's right child 5. Which code snippet correctly builds this tree?
hard
A. Node root = new Node(7); root.left = new Node(3); root.right = new Node(9); root.right.left = new Node(5);
B. Node root = new Node(7); root.left = new Node(3); root.right = new Node(9); root.left.right = new Node(5);
C. Node root = new Node(7); root.left = new Node(3); root.left.left = new Node(9); root.left.right = new Node(5);
D. Node root = new Node(7); root.right = new Node(3); root.left = new Node(9); root.left.right = new Node(5);

Solution

  1. Step 1: Create root and direct children

    Root is 7, left child 3, right child 9, so assign accordingly.
  2. Step 2: Add left child's right child

    Left child's right child is 5, so root.left.right = new Node(5); is correct.
  3. Final Answer:

    Node root = new Node(7); root.left = new Node(3); root.right = new Node(9); root.left.right = new Node(5); -> Option B
  4. Quick Check:

    Assign children correctly by following tree structure [OK]
Hint: Assign children stepwise matching tree structure [OK]
Common Mistakes:
  • Swapping left and right children
  • Assigning child to wrong parent node
  • Missing intermediate child nodes
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