Challenge - 5 Problems
Binary Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Inorder Traversal on Binary Tree
What is the output of the inorder traversal on the given binary tree?
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func inorder(root *Node) { if root == nil { return } inorder(root.Left) fmt.Print(root.Val, " -> ") inorder(root.Right) } func main() { root := &Node{Val: 1} root.Left = &Node{Val: 2} root.Right = &Node{Val: 3} root.Left.Left = &Node{Val: 4} root.Left.Right = &Node{Val: 5} inorder(root) fmt.Print("null\n") }
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree, then node, then right subtree.
✗ Incorrect
Inorder traversal visits nodes in the order: left child, current node, right child.
For the given tree, the order is 4, 2, 5, 1, 3.
❓ Predict Output
intermediate2:00remaining
Output of Preorder Traversal on Binary Tree
What is the output of the preorder traversal on the given binary tree?
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func preorder(root *Node) { if root == nil { return } fmt.Print(root.Val, " -> ") preorder(root.Left) preorder(root.Right) } func main() { root := &Node{Val: 1} root.Left = &Node{Val: 2} root.Right = &Node{Val: 3} root.Left.Left = &Node{Val: 4} root.Left.Right = &Node{Val: 5} preorder(root) fmt.Print("null\n") }
Attempts:
2 left
💡 Hint
Preorder traversal visits node first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal visits nodes in the order: current node, left child, right child.
For the given tree, the order is 1, 2, 4, 5, 3.
🔧 Debug
advanced2:00remaining
Identify the Error in Binary Tree Node Initialization
What error will this code produce when trying to create a binary tree node?
DSA Go
package main type Node struct { Val int Left *Node Right *Node } func main() { var root Node root.Val = 1 root.Left.Val = 2 }
Attempts:
2 left
💡 Hint
Check if the Left pointer is initialized before accessing its fields.
✗ Incorrect
The Left pointer is nil by default and not initialized to point to a Node.
Accessing root.Left.Val causes a runtime nil pointer dereference error.
🧠 Conceptual
advanced1:30remaining
Number of Nodes in a Full Binary Tree of Height h
What is the total number of nodes in a full binary tree of height h (where height is number of edges from root to deepest leaf)?
Attempts:
2 left
💡 Hint
A full binary tree doubles nodes at each level plus one root.
✗ Incorrect
A full binary tree has nodes count = 2^(height+1) - 1.
Height h means h edges, so levels = h+1.
🚀 Application
expert2:30remaining
Resulting Tree Structure After Insertions
Given an empty binary search tree, insert the values in this order: 10, 5, 15, 3, 7, 12, 18. What is the inorder traversal output?
Attempts:
2 left
💡 Hint
Inorder traversal of a BST outputs sorted values.
✗ Incorrect
Inserting values in BST order and then inorder traversal outputs sorted values.
The sorted order is 3, 5, 7, 10, 12, 15, 18.