Tree Terminology Root Leaf Height Depth Level in DSA Typescript - Time & Space Complexity
When working with trees, it is important to understand how the time to visit nodes grows as the tree gets bigger.
We want to know how operations like finding height or depth take longer as the tree size increases.
Analyze the time complexity of the following code snippet.
function treeHeight(node: TreeNode | null): number {
if (node === null) return -1;
const leftHeight = treeHeight(node.left);
const rightHeight = treeHeight(node.right);
return 1 + Math.max(leftHeight, rightHeight);
}
This code calculates the height of a 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 the height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The number of operations grows directly with the number of nodes.
Time Complexity: O(n)
This means the time to find the height grows linearly with the number of nodes in the tree.
[X] Wrong: "The height calculation only depends on the longest path, so it is constant time."
[OK] Correct: Even though height is about the longest path, the function must check every node to be sure, so it visits all nodes.
Understanding how tree operations scale helps you explain your approach clearly and shows you know how data size affects performance.
"What if we changed the tree to be 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 a tree and has no parent node above it.Step 2: Compare options with definition
The topmost node with no parent matches the definition exactly. Other options describe leaf nodes or specific child counts.Final Answer:
The topmost node with no parent -> Option BQuick Check:
Root = topmost node with no parent [OK]
- Confusing root with leaf node
- Thinking root must have children
- Assuming root is at the bottom
Solution
Step 1: Check correct property names and types
The children property should be an array of TreeNode objects, so type is TreeNode[].Step 2: Validate each option
interface TreeNode { value: number; children: TreeNode[]; } correctly uses 'children: TreeNode[]'. interface TreeNode { value: number; children: TreeNode; } uses single TreeNode, not array. interface TreeNode { value: number; children: number[]; } uses number array incorrectly. interface TreeNode { value: number; child: TreeNode[]; } uses wrong property name 'child'.Final Answer:
interface TreeNode { value: number; children: TreeNode[]; } -> Option AQuick Check:
Children is array of TreeNode objects [OK]
- Using singular 'child' instead of 'children'
- Setting children as single object, not array
- Using wrong type like number[] for children
const tree = {
value: 1,
children: [
{ value: 2, children: [] },
{ value: 3, children: [
{ value: 4, children: [] }
] }
]
};Solution
Step 1: Understand height definition
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 DQuick Check:
Height = longest edge count from root to leaf = 2 [OK]
- Counting nodes instead of edges for height
- Confusing height with depth
- Ignoring deeper branches
function getDepth(node: TreeNode, root: TreeNode, depth = 0): number {
if (node === root) return depth;
return getDepth(node, root.children[0], depth + 1);
}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 AQuick Check:
Recursion must explore all children, not just first [OK]
- Assuming single child covers whole tree
- Not handling multiple branches in recursion
- Confusing depth increment logic
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?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 CQuick Check:
Depth = level; height varies by subtree [OK]
- Confusing height with depth or level
- Assuming height equals level always
- Ignoring subtree size for height
Ready to prove it?
Pass the test and this pattern moves unknown
