Right Side View of Binary Tree in DSA C++ - Time & Space Complexity
We want to understand how the time needed to find the right side view of a binary tree changes as the tree grows.
How does the number of nodes affect the work done?
Analyze the time complexity of the following code snippet.
void rightSideViewHelper(TreeNode* root, int level, vector& view) {
if (!root) return;
if (level == view.size())
view.push_back(root->val);
rightSideViewHelper(root->right, level + 1, view);
rightSideViewHelper(root->left, level + 1, view);
}
vector rightSideView(TreeNode* root) {
vector view;
rightSideViewHelper(root, 0, view);
return view;
}
This code visits nodes to collect the rightmost node at each level of the tree.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Each node in the tree is visited exactly one time.
As the number of nodes increases, the function visits each node once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The work grows linearly as the tree size grows.
Time Complexity: O(n)
This means the time to find the right side view grows directly with the number of nodes in the tree.
[X] Wrong: "Since we only look at the right side, the time is less than visiting all nodes."
[OK] Correct: The code still visits every node to check if it is the rightmost at its level, so all nodes are processed.
Understanding this time complexity helps you explain how tree traversal works and shows you can analyze recursive solutions clearly.
"What if we changed the traversal to visit the left child before the right child? How would the time complexity change?"
Practice
Solution
Step 1: Understand the view perspective
The right side view shows nodes visible when looking at the tree from the right side, ignoring nodes hidden behind others.Step 2: Differentiate from other views
Unlike left side or leaf views, right side view specifically captures the rightmost nodes at each level.Final Answer:
Nodes visible when the tree is viewed from the right side -> Option BQuick Check:
Right side view = rightmost nodes [OK]
- Confusing right side view with left side view
- Thinking it shows all leaf nodes
- Assuming it shows deepest nodes only
Solution
Step 1: Check function return type
The right side view returns a list of node values, so vector<int> is appropriate.Step 2: Check parameter type
Function should take a pointer to root node (TreeNode*), not a value copy.Final Answer:
vector<int> rightSideView(TreeNode* root); -> Option AQuick Check:
Return vector<int>, parameter TreeNode* [OK]
- Passing TreeNode by value instead of pointer
- Returning int instead of vector<int>
- Using void return type incorrectly
1
/ \
2 3
\ \
5 4
Solution
Step 1: Identify rightmost nodes at each level
Level 1: Node 1; Level 2: Node 3 (right child of 1); Level 3: Node 4 (right child of 3).Step 2: Collect nodes in order
Right side view is [1, 3, 4].Final Answer:
[1, 3, 4] -> Option AQuick Check:
Rightmost nodes per level = [1, 3, 4] [OK]
- Choosing left child nodes instead of rightmost
- Including nodes hidden behind others
- Mixing node order in output
void dfs(TreeNode* node, int level, vector<int>& res) {
if (!node) return;
if (level == res.size()) res.push_back(node->val);
dfs(node->left, level + 1, res);
dfs(node->right, level + 1, res);
}
What is the main bug in this code?Solution
Step 1: Analyze traversal order
Function visits left child before right child, so first node at each level is leftmost, not rightmost.Step 2: Understand impact on right side view
Right side view requires visiting right child first to capture rightmost nodes at each level.Final Answer:
It visits left child before right, so rightmost nodes may be missed -> Option DQuick Check:
Right side view needs right-first DFS [OK]
- Visiting left child first in DFS for right side view
- Ignoring null node checks (though present here)
- Passing vector by value instead of reference
Solution
Step 1: Consider recursion limits on deep trees
Deep unbalanced trees risk stack overflow with recursion, so iterative methods are safer.Step 2: Choose traversal method for right side view
Level order traversal with a queue visits nodes level by level; capturing last node per level gives right side view.Step 3: Evaluate other options
Recursive DFS may cause stack overflow; inorder traversal does not capture right side view correctly.Final Answer:
Use iterative level order traversal with a queue, capturing last node at each level -> Option CQuick Check:
Iterative BFS with queue = safe & correct [OK]
- Using recursion on very deep trees causing stack overflow
- Visiting left child first in DFS for right side view
- Using inorder traversal which doesn't capture right side view
Ready to prove it?
Pass the test and this pattern moves unknown
