Left/Right View of Binary Tree
Problem Statement:β
Given theΒ rootΒ of a binary tree, imagine yourself standing on theΒ right sideΒ of it, returnΒ the values of the nodes you can see ordered from top to bottom.
Example 1:
Input:Β root = [1,2,3,null,5,null,4]
Output:Β [1,3,4]
Explanation:

Example 2:
Input:Β root = [1,2,3,4,null,null,null,5]
Output:Β [1,3,4,5]
Explanation:

-
Example:
β Solution 1: Left View (Recursive DFS)β
class Solution {
public:
void collectLeftView(TreeNode* node, int level, vector<int>& view) {
if (!node) return;
// Add the first node at this level
if (view.size() == level) {
view.push_back(node->val);
}
// Traverse left first to ensure leftmost is picked
collectLeftView(node->left, level + 1, view);
collectLeftView(node->right, level + 1, view);
}
vector<int> leftSideView(TreeNode* root) {
vector<int> leftView;
collectLeftView(root, 0, leftView);
return leftView;
}
};
β Solution 2: Right View (Recursive DFS)β
class Solution {
public:
void collectRightView(TreeNode* node, int level, vector<int>& view) {
if (!node) return;
// Add the first node at this level
if (view.size() == level) {
view.push_back(node->val);
}
// Traverse right first to ensure rightmost is picked
collectRightView(node->right, level + 1, view);
collectRightView(node->left, level + 1, view);
}
vector<int> rightSideView(TreeNode* root) {
vector<int> rightView;
collectRightView(root, 0, rightView);
return rightView;
}
};
β Solution 3: Left View (Iterative BFS)β
class Solution {
public:
vector<int> leftSideView(TreeNode* root) {
vector<int> leftView;
if (!root) return leftView;
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty()) {
int levelSize = nodeQueue.size();
for (int i = 0; i < levelSize; ++i) {
TreeNode* current = nodeQueue.front();
nodeQueue.pop();
if (i == 0) leftView.push_back(current->val); // first node at this level
if (current->left) nodeQueue.push(current->left);
if (current->right) nodeQueue.push(current->right);
}
}
return leftView;
}
};
β Solution 4: Right View (Iterative BFS)β
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> rightView;
if (!root) return rightView;
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty()) {
int levelSize = nodeQueue.size();
for (int i = 0; i < levelSize; ++i) {
TreeNode* current = nodeQueue.front();
nodeQueue.pop();
if (i == levelSize - 1) rightView.push_back(current->val); // last node at this level
if (current->left) nodeQueue.push(current->left);
if (current->right) nodeQueue.push(current->right);
}
}
return rightView;
}
};
π How It Worksβ
- In DFS, we keep track of the level during recursion.
- For left view, go left first and record the first node seen at that level.
- For right view, go right first and do the same.
- In BFS, we use level-order traversal.
- For left view, pick the first node in each level.
- For right view, pick the last node in each level.
π§© Key Observationβ
- Use a queue for BFS.
- Use recursion + level for DFS.
- For any side view: track the first/last node at each level.
β±οΈ Time & Space Complexityβ
| Approach | Time | Space |
|---|---|---|
| DFS (left/right) | O(N) | O(H) |
| BFS (left/right) | O(N) | O(W) |
Where:
- N is total number of nodes
- H is tree height (recursion stack)
- W is tree width (queue size)
β οΈ Edge Casesβ
- Empty tree β returns an empty vector.
- Skewed trees (left/right) β returns all nodes linearly.
- Multiple nodes at same depth β only one (first/last) considered depending on view.
π Related Problemsβ
- Top View of Binary Tree
- Bottom View of Binary Tree
- Vertical Order Traversal
- Diagonal Traversal
π Level Up Your Engineering Mindset
Never Study in Isolation. Join Binary Dose.
Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.
Official Hub
Discord Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.