Skip to main content

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​

ApproachTimeSpace
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.

  • 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.

Discord
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚑
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

πŸ’‘Have handwritten notes, cheat-sheets, or want to publish a technical article?
Learn How to Contribute→