Bird
Raised Fist0
DSA Goprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Binary Tree Builder 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 and Inorder Traversal
What is the output of the inorder traversal after manually creating this binary tree?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    left  *Node
    right *Node
}

func inorder(root *Node) {
    if root == nil {
        return
    }
    inorder(root.left)
    fmt.Print(root.value, " ")
    inorder(root.right)
}

func main() {
    root := &Node{value: 10}
    root.left = &Node{value: 5}
    root.right = &Node{value: 15}
    root.left.left = &Node{value: 3}
    root.left.right = &Node{value: 7}
    root.right.right = &Node{value: 18}

    inorder(root)
}
A3 5 7 10 15 18
B10 5 3 7 15 18
C18 15 10 7 5 3
D5 3 7 10 15 18
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree, then root, then right subtree.
❓ Predict Output
intermediate
2:00remaining
Output of Preorder Traversal on Manually Created Binary Tree
What is the output of the preorder traversal after manually creating this binary tree?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    left  *Node
    right *Node
}

func preorder(root *Node) {
    if root == nil {
        return
    }
    fmt.Print(root.value, " ")
    preorder(root.left)
    preorder(root.right)
}

func main() {
    root := &Node{value: 8}
    root.left = &Node{value: 4}
    root.right = &Node{value: 12}
    root.left.left = &Node{value: 2}
    root.left.right = &Node{value: 6}
    root.right.left = &Node{value: 10}
    root.right.right = &Node{value: 14}

    preorder(root)
}
A2 4 6 8 10 12 14
B8 4 2 6 12 10 14
C14 12 10 8 6 4 2
D4 2 6 8 12 10 14
Attempts:
2 left
💡 Hint
Preorder traversal visits root first, then left subtree, then right subtree.
🔧 Debug
advanced
2:00remaining
Identify the Error in Manual Binary Tree Node Linking
What error will occur when running this code that manually creates a binary tree?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    left  *Node
    right *Node
}

func main() {
    root := &Node{value: 1}
    root.left = &Node{value: 2}
    root.right = &Node{value: 3}
    root.left.left = &Node{value: 4}
    root.left.right = &Node{value: 5}
    root.right.left = &Node{value: 6}
    root.right.right = &Node{value: 7}

    fmt.Println(root.left.right.right.value)
}
AOutput: 7
BOutput: 5
Cruntime error: invalid memory address or nil pointer dereference
DCompilation error: undefined field
Attempts:
2 left
💡 Hint
Check if root.left.right.right is assigned before accessing its value.
🧠 Conceptual
advanced
1:00remaining
Number of Nodes in a Manually Created Binary Tree
If you manually create a binary tree with root and assign left and right children only to the root, how many nodes does the tree have?
A4
B2
C1
D3
Attempts:
2 left
💡 Hint
Count the root plus its left and right children.
🚀 Application
expert
3:00remaining
Constructing a Binary Tree and Printing Level Order Traversal
Given the manual creation of a binary tree below, what is the output of the level order traversal?
DSA Go
package main

import (
    "container/list"
    "fmt"
)

type Node struct {
    value int
    left  *Node
    right *Node
}

func levelOrder(root *Node) {
    if root == nil {
        return
    }
    queue := list.New()
    queue.PushBack(root)

    for queue.Len() > 0 {
        element := queue.Front()
        queue.Remove(element)
        node := element.Value.(*Node)
        fmt.Print(node.value, " ")
        if node.left != nil {
            queue.PushBack(node.left)
        }
        if node.right != nil {
            queue.PushBack(node.right)
        }
    }
}

func main() {
    root := &Node{value: 20}
    root.left = &Node{value: 10}
    root.right = &Node{value: 30}
    root.left.left = &Node{value: 5}
    root.left.right = &Node{value: 15}
    root.right.right = &Node{value: 40}

    levelOrder(root)
}
A20 10 30 5 15 40
B10 20 30 5 15 40
C20 30 10 40 15 5
D5 10 15 20 30 40
Attempts:
2 left
💡 Hint
Level order traversal visits nodes level by level from left to right.

Practice

(1/5)
1. What is the first step to manually create a binary tree in Go?
easy
A. Define a struct type for the tree node with data and pointers
B. Write a function to traverse the tree
C. Initialize an empty slice
D. Import the fmt package

Solution

  1. Step 1: Define the node structure

    To create a binary tree manually, you first need a struct that represents each node with fields for data and pointers to left and right children.
  2. Step 2: Use this struct to create nodes and link them

    After defining the struct, you can create nodes and connect them to form the tree.
  3. Final Answer:

    Define a struct type for the tree node with data and pointers -> Option A
  4. Quick Check:

    Node struct definition = A [OK]
Hint: Start by defining the node struct with left and right pointers [OK]
Common Mistakes:
  • Trying to create nodes without defining the struct first
  • Confusing slices with tree nodes
  • Skipping pointer fields in the struct
2. Which of the following is the correct syntax to declare a binary tree node struct in Go?
easy
A. class Node { int data; Node left; Node right }
B. struct Node { int data; Node* left; Node* right }
C. Node := struct { data int; left Node; right Node }
D. type Node struct { data int; left *Node; right *Node }

Solution

  1. Step 1: Understand Go struct syntax

    In Go, structs are declared with the keyword 'type' followed by the struct name and fields inside curly braces. Pointers use '*'.
  2. Step 2: Match the correct syntax

    type Node struct { data int; left *Node; right *Node } correctly uses 'type Node struct { data int; left *Node; right *Node }' which is valid Go syntax for a binary tree node.
  3. Final Answer:

    type Node struct { data int; left *Node; right *Node } -> Option D
  4. Quick Check:

    Go struct with pointers = C [OK]
Hint: Remember Go uses 'type' and '*' for pointers in structs [OK]
Common Mistakes:
  • Using C/C++ or Java syntax instead of Go
  • Missing the '*' for pointer fields
  • Using := inside type declaration
3. What will be the output of this Go code that manually creates a binary tree and prints the root node's data?
package main
import "fmt"
type Node struct {
  data int
  left *Node
  right *Node
}
func main() {
  root := &Node{data: 10}
  root.left = &Node{data: 5}
  root.right = &Node{data: 15}
  fmt.Println(root.data)
}
medium
A. 10
B. 5
C. 15
D. Compilation error

Solution

  1. Step 1: Understand node creation

    The root node is created with data 10, and its left and right children are assigned 5 and 15 respectively.
  2. Step 2: Identify printed value

    The code prints root.data, which is 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    root.data = 10 [OK]
Hint: Print root.data to get the root node's value [OK]
Common Mistakes:
  • Printing left or right data instead of root
  • Confusing pointer dereferencing syntax
  • Expecting traversal output instead of root data
4. Identify the error in this Go code that tries to create a binary tree node:
type Node struct {
  data int
  left *Node
  right *Node
}
func main() {
  root := Node{data: 10}
  root.left = Node{data: 5}
  root.right = Node{data: 15}
}
medium
A. Missing semicolon after struct definition
B. Data field should be string, not int
C. Cannot assign struct to pointer field without & operator
D. No error, code is correct

Solution

  1. Step 1: Check pointer assignments

    The left and right fields are pointers (*Node), but the code assigns Node structs directly without using & to get their address.
  2. Step 2: Correct pointer assignment

    To fix, use &Node{data: 5} and &Node{data: 15} to assign pointers.
  3. Final Answer:

    Cannot assign struct to pointer field without & operator -> Option C
  4. Quick Check:

    Pointer fields need & before struct [OK]
Hint: Use & to assign address to pointer fields [OK]
Common Mistakes:
  • Assigning struct value directly to pointer field
  • Ignoring pointer type in struct fields
  • Assuming Go auto-converts struct to pointer
5. Given the following Go code to create a binary tree manually, what is the correct way to add a new node with value 7 as the left child of the node with value 5?
type Node struct {
  data int
  left *Node
  right *Node
}
func main() {
  root := &Node{data: 10}
  root.left = &Node{data: 5}
  root.right = &Node{data: 15}
  // Add new node here
}
hard
A. root.right.right = &Node{data: 7}
B. root.left.left = &Node{data: 7}
C. root.right.left = &Node{data: 7}
D. root.left.right = &Node{data: 7}

Solution

  1. Step 1: Locate node with value 5

    The node with value 5 is root.left.
  2. Step 2: Add new node as left child of node 5

    To add a left child, assign root.left.left = &Node{data: 7}.
  3. Final Answer:

    root.left.left = &Node{data: 7} -> Option B
  4. Quick Check:

    Left child of node 5 = D [OK]
Hint: Use root.left.left to add left child to node 5 [OK]
Common Mistakes:
  • Adding new node to wrong child pointer
  • Confusing left and right pointers
  • Assigning to root.right instead of root.left
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