Tree Terminology Root Leaf Height Depth Level in DSA Javascript - Time & Space Complexity
We want to understand how the time to explore a tree changes as the tree grows.
How does the number of steps grow when we visit nodes like root, leaves, or measure height and depth?
Analyze the time complexity of the following code snippet.
function treeHeight(node) {
if (!node) return -1;
const leftHeight = treeHeight(node.left);
const rightHeight = treeHeight(node.right);
return Math.max(leftHeight, rightHeight) + 1;
}
This code finds the height of a binary tree by checking the height of left and right subtrees recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Once per node in the tree.
As the tree grows, the function visits every node once to find height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The number of steps grows directly with the number of nodes.
Time Complexity: O(n)
This means the time to find height grows linearly with the number of nodes in the tree.
[X] Wrong: "The height calculation only depends on the longest path, so it takes less time than visiting all nodes."
[OK] Correct: Even to find the longest path, the function must check every node to be sure no longer path exists.
Understanding how tree operations scale helps you explain your approach clearly and confidently in interviews.
"What if we changed the tree to a linked list (all nodes have only one child)? How would the time complexity change?"
Practice
root?Solution
Step 1: Understand the root node definition
The root node is the starting point of the tree and has no parent node above it.Step 2: Compare options with definition
The topmost node with no parent matches this definition exactly; other options describe leaf or internal nodes.Final Answer:
The topmost node with no parent -> Option AQuick Check:
Root = topmost node with no parent [OK]
- Confusing root with leaf nodes
- Thinking root must have children
- Assuming root is at the bottom
leaf node in a tree?Solution
Step 1: Define leaf node
A leaf node is a node that does not have any children; it is at the end of a branch.Step 2: Match options to definition
Only A node with no children states the node has no children, which matches the leaf node definition.Final Answer:
A node with no children -> Option CQuick Check:
Leaf = node with no children [OK]
- Thinking leaf nodes have children
- Confusing leaf with root
- Assuming leaf nodes must have one child
A
/ \
B C
/ / \
D E F
What is the
height of node B?Solution
Step 1: Understand height of a node
Height is the number of edges on the longest path from the node down to a leaf.Step 2: Calculate height of node B
Node B has one child D which is a leaf. The longest path from B to leaf is B->D with 1 edge.Final Answer:
1 -> Option AQuick Check:
Height of B = 1 edge to leaf D [OK]
- Counting nodes instead of edges
- Confusing height with depth
- Counting path to root instead of leaf
X
/ \
Y Z
/
W
What is the
depth of node W?Note: Depth is the number of edges from the root to the node.
Solution
Step 1: Understand depth definition
Depth is the number of edges from the root node to the target node.Step 2: Count edges from root X to W
Path is X -> Y -> W, which has 2 edges.Final Answer:
2 -> Option BQuick Check:
Depth of W = 2 edges from root [OK]
- Counting nodes instead of edges
- Confusing depth with height
- Starting count from 1 instead of 0 edges
level of a node is defined as its depth plus one.Given the tree:
M
/ \
N O
/ \
P Q
What is the level of node
P?Solution
Step 1: Calculate depth of node P
Path from root M to P is M -> O -> P, which has 2 edges, so depth = 2.Step 2: Calculate level using formula
Level = depth + 1 = 2 + 1 = 3.Final Answer:
3 -> Option DQuick Check:
Level = depth + 1 = 3 [OK]
- Using depth as level without adding 1
- Counting nodes incorrectly
- Confusing level with height
Ready to prove it?
Pass the test and this pattern moves unknown
