Challenge - 5 Problems
Tree Terminology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Tree Node Levels
What is the output of the following code that prints the level of each node in a simple tree?
DSA Javascript
class Node { constructor(value) { this.value = value; this.children = []; } } const root = new Node(1); const child1 = new Node(2); const child2 = new Node(3); const grandchild = new Node(4); root.children.push(child1, child2); child1.children.push(grandchild); function printLevels(node, level = 0) { console.log(`${node.value}: Level ${level}`); for (const child of node.children) { printLevels(child, level + 1); } } printLevels(root);
Attempts:
2 left
💡 Hint
Think about the root node level and how it increases for children.
✗ Incorrect
The root node starts at level 0. Each child node is one level deeper than its parent. So, node 1 is level 0, nodes 2 and 3 are level 1, and node 4 is level 2.
🧠 Conceptual
intermediate1:00remaining
Identify Leaf Nodes in a Tree
Which of the following best describes a leaf node in a tree?
Attempts:
2 left
💡 Hint
Leaf nodes do not have any branches going out.
✗ Incorrect
A leaf node is a node that does not have any children. It is the end point of a branch in the tree.
❓ Predict Output
advanced2:00remaining
Calculate Height of a Tree
What is the output of the following code that calculates the height of a tree?
DSA Javascript
class Node { constructor(value) { this.value = value; this.children = []; } } const root = new Node(10); const c1 = new Node(20); const c2 = new Node(30); const c3 = new Node(40); const gc1 = new Node(50); const gc2 = new Node(60); root.children.push(c1, c2); c1.children.push(c3); c3.children.push(gc1, gc2); function height(node) { if (node.children.length === 0) return 0; let maxChildHeight = 0; for (const child of node.children) { const childHeight = height(child); if (childHeight > maxChildHeight) maxChildHeight = childHeight; } return maxChildHeight + 1; } console.log(height(root));
Attempts:
2 left
💡 Hint
Height is the longest path from root to leaf in edges.
✗ Incorrect
The longest path from root (10) to leaf is 10 -> 20 -> 40 -> 50 or 60, which has 3 edges, so height is 3.
🔧 Debug
advanced2:00remaining
Find the Error in Depth Calculation
The following code tries to calculate the depth of a node in a tree but produces incorrect results. What is the error?
DSA Javascript
class Node { constructor(value) { this.value = value; this.parent = null; } } const root = new Node(1); const child = new Node(2); child.parent = root; const grandchild = new Node(3); grandchild.parent = child; function depth(node) { let d = 0; while (node.parent !== null) { d += 1; } return d; } console.log(depth(grandchild));
Attempts:
2 left
💡 Hint
Check if the loop changes the node variable to move up the tree.
✗ Incorrect
The code never updates the node inside the while loop, so the condition never changes and causes an infinite loop.
🧠 Conceptual
expert1:30remaining
Understanding Tree Levels and Depths
In a tree, which statement correctly relates the level and depth of a node?
Attempts:
2 left
💡 Hint
Think about how level and depth are counted starting from root.
✗ Incorrect
Depth counts edges from root to node starting at 0 for root. Level counts nodes on the path starting at 1 for root. So level = depth + 1.