Challenge - 5 Problems
Binary Tree Builder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree, then root, then right subtree.
✗ Incorrect
Inorder traversal prints nodes in ascending order for a binary search tree. The tree nodes are 3, 5, 7, 10, 15, 18 in that order.
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Preorder traversal visits root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal prints nodes starting from root, then left subtree, then right subtree. The order is 8, 4, 2, 6, 12, 10, 14.
🔧 Debug
advanced2: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) }
Attempts:
2 left
💡 Hint
Check if root.left.right.right is assigned before accessing its value.
✗ Incorrect
The node root.left.right.right is nil because it was never assigned. Accessing its value causes a nil pointer dereference runtime error.
🧠 Conceptual
advanced1: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?
Attempts:
2 left
💡 Hint
Count the root plus its left and right children.
✗ Incorrect
The tree has the root node plus one left child and one right child, totaling 3 nodes.
🚀 Application
expert3: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) }
Attempts:
2 left
💡 Hint
Level order traversal visits nodes level by level from left to right.
✗ Incorrect
Level order traversal visits nodes in this order: root (20), then level 2 (10, 30), then level 3 (5, 15, 40).