Tree Terminology Root Leaf Height Depth Level in DSA C# - Time & Space Complexity
We want to understand how the time to work with a tree changes as the tree grows.
Specifically, how operations related to root, leaf, height, depth, and level scale with tree size.
Analyze the time complexity of the following code snippet.
public class TreeNode {
public int Value;
public TreeNode Left, Right;
public TreeNode(int val) { Value = val; }
}
int GetHeight(TreeNode node) {
if (node == null) return 0;
return 1 + Math.Max(GetHeight(node.Left), GetHeight(node.Right));
}
This code calculates the height of a binary tree by checking the longest path from the root to any leaf.
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 calls |
| 100 | About 100 calls |
| 1000 | About 1000 calls |
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: "Calculating height only needs to check one path from root to leaf."
[OK] Correct: The height depends on the longest path, so all nodes must be checked to be sure.
Understanding how tree operations scale helps you explain your approach clearly and confidently in interviews.
"What if we stored the height at each node during insertion? How would the time complexity of getting height change?"
Practice
root?Solution
Step 1: Understand the root node concept
The root node is the starting point of a tree and has no parent node above it.Step 2: Identify the correct description
Among the options, only the topmost node with no parent matches the root node definition.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 any node at the bottom
Solution
Step 1: Check for correct data types and access modifiers
The node should have a public integer value and a list of child nodes. class Node { public int Value; public List<Node> Children; } uses List<Node> which is correct.Step 2: Eliminate incorrect options
class Node { int Value; int Children; } uses int for children which is wrong. class Node { public int Value; public Node Children; } uses a single Node for children instead of a list. class Node { public int Value; public Node[] Children; } uses an array which is valid but less flexible than List.Final Answer:
class Node { public int Value; public List<Node> Children; } -> Option CQuick Check:
Use List<Node> for children in C# [OK]
- Using int instead of List<Node> for children
- Missing public access modifier
- Using single Node instead of collection for children
Solution
Step 1: Understand level definition
Level of a node is the number of edges from the root to that node, starting at 0 for root.Step 2: Calculate leaf node level
If leaf is 4 edges away from root, its level is 4.Final Answer:
4 -> Option BQuick Check:
Level = edges from root = 4 [OK]
- Confusing level with height
- Starting level count at 1 instead of 0
- Mixing depth and level definitions
Node root = new Node { Value = 1, Children = new List<Node> {
new Node { Value = 2, Children = new List<Node> {
new Node { Value = 4, Children = new List<Node>() },
new Node { Value = 5, Children = new List<Node>() }
}},
new Node { Value = 3, Children = new List<Node>() }
}};What is the height of this tree?
Solution
Step 1: Understand height definition
Height is the number of edges on the longest path from the root to a leaf node.Step 2: Trace the longest path in the tree
Longest path: root(1) -> 2 -> 4 (or 5). This path has 2 edges, so height = 2.Step 3: Reconsider height counting
Height is often defined as the number of nodes on the longest path minus 1, which equals the number of edges. The path has 3 nodes (1,2,4), so height = 2 edges, which is correct. However, some definitions count height as number of nodes, which would be 3.Step 4: Confirm standard definition
Standard definition: height = number of edges = 2.Final Answer:
2 -> Option AQuick Check:
Height = longest path edges = 2 [OK]
- Counting nodes instead of edges for height
- Confusing height with depth
- Ignoring leaf nodes at bottom
int GetDepth(Node node) {
if (node == null) return 0;
return 1 + GetDepth(node.Children[0]);
}What is the main problem with this code?
Solution
Step 1: Analyze recursion logic
The function calls itself only on the first child (Children[0]), ignoring other children.Step 2: Identify the bug impact
This means it does not calculate depth correctly if node has multiple children; it misses deeper paths.Final Answer:
It only checks the first child, ignoring others -> Option DQuick Check:
Check all children for correct depth [OK]
- Assuming single child trees only
- Ignoring null checks for children list
- Confusing depth with height
Ready to prove it?
Pass the test and this pattern moves unknown
