What is the depth of a node (depth)?
A node's depth is the distance from the root to that node, measured as the number of edges between them.
Formally:
- The root's depth = 0 (since there are 0 edges from the root to itself).
- The depth of any other node = the depth of its parent + 1.
Example:
javascript
A
/ \
B C
/ \
D EA→ depth 0B, C→ depth 1D, E→ depth 2
Difference from height:
- Depth is how many steps you take down from the root to a node.
- Height is how many steps up from a node to the deepest leaf.
Summary: A node's depth shows how deep it is in the tree relative to the root.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.