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 printed level of each node after running this code?
DSA Typescript
class TreeNode { value: number; children: TreeNode[]; level: number; constructor(value: number) { this.value = value; this.children = []; this.level = 0; } } function assignLevels(root: TreeNode, currentLevel: number = 0): void { root.level = currentLevel; for (const child of root.children) { assignLevels(child, currentLevel + 1); } } const root = new TreeNode(1); const child1 = new TreeNode(2); const child2 = new TreeNode(3); const grandchild = new TreeNode(4); root.children.push(child1, child2); child1.children.push(grandchild); assignLevels(root); console.log(root.level, child1.level, child2.level, grandchild.level);
Attempts:
2 left
š” Hint
Think about how levels increase as you go down from the root.
ā Incorrect
The root node starts at level 0. Each child node has a level one greater than its parent. So, child1 and child2 are at level 1, and grandchild is at level 2.
š§ Conceptual
intermediate1:30remaining
Identify Leaf Nodes in a Tree
Which nodes are leaf nodes in the following tree structure?
Root (1)
āā Child A (2)
ā āā Grandchild A1 (4)
āā Child B (3)
āā Grandchild B1 (5)
āā Grandchild B2 (6)
Attempts:
2 left
š” Hint
Leaf nodes have no children.
ā Incorrect
Leaf nodes are nodes without any children. Here, nodes 4, 5, and 6 have no children, so they are leaves.
ā Predict Output
advanced2:00remaining
Calculate Height of a Tree
What is the output of the height calculated by this code for the given tree?
DSA Typescript
class TreeNode { value: number; children: TreeNode[]; constructor(value: number) { this.value = value; this.children = []; } } function height(node: TreeNode | null): number { if (!node) return -1; if (node.children.length === 0) return 0; let maxChildHeight = -1; for (const child of node.children) { const childHeight = height(child); if (childHeight > maxChildHeight) maxChildHeight = childHeight; } return maxChildHeight + 1; } const root = new TreeNode(1); const c1 = new TreeNode(2); const c2 = new TreeNode(3); const gc1 = new TreeNode(4); const gc2 = new TreeNode(5); root.children.push(c1, c2); c1.children.push(gc1); c2.children.push(gc2); console.log(height(root));
Attempts:
2 left
š” Hint
Height is the longest path from root to a leaf in edges.
ā Incorrect
The longest path from root to leaf is 2 edges (root->c1->gc1 or root->c2->gc2). So height is 2.
š§ Debug
advanced2:00remaining
Find the Depth of a Node
What is the depth of node with value 5 after running this code?
DSA Typescript
class TreeNode { value: number; children: TreeNode[]; constructor(value: number) { this.value = value; this.children = []; } } function findDepth(root: TreeNode, target: number, currentDepth: number = 0): number { if (root.value === target) return currentDepth; for (const child of root.children) { const d = findDepth(child, target, currentDepth + 1); if (d !== -1) return d; } return -1; } const root = new TreeNode(1); const c1 = new TreeNode(2); const c2 = new TreeNode(3); const gc1 = new TreeNode(4); const gc2 = new TreeNode(5); root.children.push(c1, c2); c1.children.push(gc1); c2.children.push(gc2); console.log(findDepth(root, 5));
Attempts:
2 left
š” Hint
Depth counts edges from root to the node.
ā Incorrect
Node 5 is a child of node 3, which is a child of root (1). So depth is 2.
š§ Conceptual
expert1:30remaining
Level vs Depth vs Height
Which statement correctly describes the difference between level, depth, and height of a node in a tree?
Attempts:
2 left
š” Hint
Think about how far a node is from the root and from the leaves.
ā Incorrect
Level and depth both mean the distance from the root to the node (counting edges). Height is the longest distance from the node down to any leaf.