0
0
DSA Goprogramming~20 mins

Tree Terminology Root Leaf Height Depth Level in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tree Terminology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:00remaining
Identify the root node in a tree
In a tree data structure, which node is called the root?
AThe node with the highest depth
BThe node with no children
CThe node with the lowest height
DThe node with no parent
Attempts:
2 left
💡 Hint
Think about where the tree starts growing from.
Predict Output
intermediate
1: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.
A[4, 5, 6]
B[1, 2, 3]
C[2, 3, 6]
D[1, 4, 5]
Attempts:
2 left
💡 Hint
Leaves have no children nodes.
Predict Output
advanced
1: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.
A0
B3
C1
D2
Attempts:
2 left
💡 Hint
Count edges from node 2 down to its farthest leaf.
🧠 Conceptual
advanced
1:00remaining
Understanding depth and level of a node
Which statement correctly describes the depth and level of a node in a tree?
ADepth is the number of leaves under the node; level is the number of ancestors
BDepth is the number of edges from the root to the node; level is depth plus one
CDepth and level are always equal to the height of the node
DDepth is the number of children of the node; level is the number of siblings
Attempts:
2 left
💡 Hint
Think about how far a node is from the root.
🔧 Debug
expert
2: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.
AReturns incorrect height for leaf nodes
BCauses infinite recursion
CReturns -1 for empty tree correctly
DPanics due to nil pointer dereference
Attempts:
2 left
💡 Hint
Check base case and initial maxHeight value.