Challenge - 5 Problems
Tree Terminology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:00remaining
Identify the root node in a tree
In a tree data structure, which node is called the root?
Attempts:
2 left
💡 Hint
Think about where the tree starts growing from.
✗ Incorrect
The root node is the topmost node in a tree and has no parent node.
❓ Predict Output
intermediate1:30remaining
Output the leaf nodes of a tree
Given the following tree structure, which nodes are leaves?
DSA Go
type Node struct { Value int Children []*Node } // Tree: // 1 // / \ // 2 3 // / \ \ // 4 5 6 // Leaves are nodes with no children.
Attempts:
2 left
💡 Hint
Leaves have no children nodes.
✗ Incorrect
Nodes 4, 5, and 6 have no children, so they are leaves.
❓ Predict Output
advanced1:30remaining
Calculate the height of a node in a tree
What is the height of node 2 in the following tree?
// Tree:
// 1
// / \
// 2 3
// / \ \
// 4 5 6
// Height is the number of edges on the longest path from the node to a leaf.
Attempts:
2 left
💡 Hint
Count edges from node 2 down to its farthest leaf.
✗ Incorrect
Node 2 has children 4 and 5 which are leaves. The longest path from 2 to a leaf has 1 edge (to 4 or 5), so height is 1.
🧠 Conceptual
advanced1:00remaining
Understanding depth and level of a node
Which statement correctly describes the depth and level of a node in a tree?
Attempts:
2 left
💡 Hint
Think about how far a node is from the root.
✗ Incorrect
Depth counts edges from root to node; level counts nodes from root (starting at 1). So level = depth + 1.
🔧 Debug
expert2:00remaining
Find the error in height calculation code
What error will this Go code produce when calculating the height of a tree node?
func height(node *Node) int {
if node == nil {
return -1
}
maxHeight := -1
for _, child := range node.Children {
h := height(child)
if h > maxHeight {
maxHeight = h
}
}
return maxHeight + 1
}
// Node struct has Children slice.
Attempts:
2 left
💡 Hint
Check base case and initial maxHeight value.
✗ Incorrect
For leaf nodes (no children), maxHeight is initialized to -1 and remains -1, so the function returns 0. This correctly represents the height of a leaf node. The original code initialized maxHeight to 0, causing the function to return 1 for leaf nodes, which is incorrect.