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.
How does the size and shape of a tree affect the time to reach nodes like root, leaves, or calculate height and depth?
Analyze the time complexity of this simple function that finds the height of a binary tree.
int height(Node* root) {
if (root == nullptr) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}
This function finds the height by checking the longest path from the root to any leaf.
Look for repeated work in the code.
- 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.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls |
| 100 | About 100 calls |
| 1000 | About 1000 calls |
Pattern observation: The work 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 function only checks the longest path, so it runs in constant time."
[OK] Correct: Even though it finds the longest path, it must visit every node to be sure, so it takes time proportional to all nodes.
Understanding how tree size affects operations like height or depth helps you explain your code clearly and shows you know how data grows in real problems.
"What if the tree was balanced versus very unbalanced? How would that affect the time complexity of finding height?"
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: Differentiate root from other nodes
Leaf nodes have no children, but root has no parent. Bottom level nodes are leaves or internal nodes, not necessarily root.Final Answer:
The topmost node with no parent -> Option CQuick Check:
Root = topmost node with no parent [OK]
- Confusing root with leaf node
- Thinking root must have children
- Assuming root is at bottom level
Solution
Step 1: Check pointer usage for child nodes
Child nodes must be pointers to Node to allow recursive structure.Step 2: Validate data types of members
Data is int, left and right are pointers to Node, so struct Node { int data; Node* left; Node* right; }; is correct.Final Answer:
struct Node { int data; Node* left; Node* right; }; -> Option AQuick Check:
Child nodes use pointers in struct [OK]
- Using Node instead of Node* for children
- Mixing int and Node types for children
- Missing semicolon at struct end
10
/ \
5 15
/ / \
3 12 20
What is the height of the node with value 5?Solution
Step 1: Understand height definition
Height of a node is the number of edges on the longest path from that node down to a leaf.Step 2: Calculate height of node 5
Node 5 has left child 3 (leaf). Longest path from 5 to leaf is 5 -> 3, which is 1 edge. Height of 3 is 0. Height of 5 = 1 + max height of children = 1 + 0 = 1. However, node 5 has no right child, so max height is 1. So height is 1. But the question asks for height of node 5. Since 3 is leaf (height 0), height of 5 is 1. But 2 says 2, which is incorrect. So correct answer is 1 (1).Final Answer:
1 -> Option AQuick Check:
Height = edges to deepest leaf = 1 [OK]
- Counting nodes instead of edges
- Confusing height with depth
- Ignoring missing right child
struct Node {
int data;
Node* left;
Node* right;
};
Node* root = new Node{10, nullptr, nullptr};
root->left = new Node{5, nullptr, nullptr};
root->left->left = new Node{3, nullptr, nullptr};
root->right = new Node{15, nullptr, nullptr};
root->right->right = new Node{20, nullptr, nullptr};
root->right->left = 12;
Solution
Step 1: Analyze the last assignment
Lineroot->right->left = 12;assigns integer 12 to a pointer Node* left, which is invalid.Step 2: Check pointer assignment rules
Pointers must be assigned addresses or nullptr, not raw integers.Final Answer:
Assigning int 12 to a Node* pointer -> Option DQuick Check:
Pointer must hold address, not int [OK]
- Ignoring type mismatch in assignment
- Thinking nullptr and NULL difference causes error
- Overlooking semicolon presence
depth of a node is defined as the number of edges from the root to that node. Given the tree: 1
/ \
2 3
/ / \
4 5 6
/
7
What is the depth of node 7 and what is its level?Solution
Step 1: Calculate depth of node 7
Path from root (1) to 7 is 1 -> 3 -> 5 -> 7, which has 3 edges, so depth = 3.Step 2: Calculate level of node 7
Level is depth + 1, so level = 3 + 1 = 4.Final Answer:
Depth = 3, Level = 4 -> Option BQuick Check:
Level = depth + 1 = 4 [OK]
- Confusing depth with level
- Counting nodes instead of edges for depth
- Starting depth count from 0 or 1 incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
