Height is the longest path from root to any leaf node, counting edges.
Step 2: Trace longest path in given tree
Root (1) to node (3) to node (4) is longest path with 2 edges, so height = 2. Counting nodes gives 3 levels, but height counts edges, so height = 2.
Final Answer:
2 -> Option D
Quick Check:
Height = longest edge count from root to leaf = 2 [OK]
Hint: Height counts edges, not nodes, from root to deepest leaf [OK]
Common Mistakes:
Counting nodes instead of edges for height
Confusing height with depth
Ignoring deeper branches
4. Consider this TypeScript code snippet for calculating the depth of a node in a tree. Identify the bug:
function getDepth(node: TreeNode, root: TreeNode, depth = 0): number {
if (node === root) return depth;
return getDepth(node, root.children[0], depth + 1);
}
medium
A. It always checks only the first child, missing other branches
B. It never increments depth
C. It compares node and root incorrectly
D. It should return depth + 1 instead of depth
Solution
Step 1: Analyze recursive call logic
The function always calls getDepth on root.children[0], ignoring other children, so it misses nodes not in first child's subtree.
Step 2: Understand impact on depth calculation
This causes incorrect depth for nodes not in first child's branch, as they are never reached.
Final Answer:
It always checks only the first child, missing other branches -> Option A
Quick Check:
Recursion must explore all children, not just first [OK]
Hint: Check if recursion covers all children, not just one [OK]
Common Mistakes:
Assuming single child covers whole tree
Not handling multiple branches in recursion
Confusing depth increment logic
5. You have a tree where each node has a level property indicating its distance from the root (root level = 0). If a node has level 3, what is its depth and height relative to the root?
hard
A. Depth = 0, Height = 3
B. Depth = 3, Height = 3
C. Depth = 3, Height depends on subtree size
D. Depth and Height both equal to 0
Solution
Step 1: Understand level and depth relation
Level and depth both measure distance from root, so depth = level = 3.
Step 2: Understand height meaning
Height is longest path from node down to leaf, which depends on subtree size, so height is unknown from level alone.
Final Answer:
Depth = 3, Height depends on subtree size -> Option C
Quick Check:
Depth = level; height varies by subtree [OK]
Hint: Level = depth; height depends on node's subtree [OK]
Common Mistakes:
Confusing height with depth or level
Assuming height equals level always
Ignoring subtree size for height
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.