0
0
DSA Goprogramming~20 mins

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

Choose your learning style9 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.