199. Binary Tree Right Side View
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Trees
- Time
- O(N)
- Space
- O(H)
Standard Solution (C++)
SummaryGiven the root of a binary tree, return the values of the nodes visible when the tree is viewed from the right side — one value per level, specifically the rightmost node at each depth.
Approach
- Perform a BFS (level-order traversal) using a queue, processing all nodes level by level.
- After dequeuing every node in a level, the last node dequeued is the rightmost visible node; record its value.
- Enqueue left child before right child so that right children are naturally processed last within each level.
- This gives exactly one value per depth in O(n) time with no recursion stack overhead.
#include <vector>
#include <queue>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
if (!root) return result;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int levelSize = q.size();
int rightmost = 0;
for (int i = 0; i < levelSize; ++i) {
TreeNode* node = q.front();
q.pop();
// Track the last value seen at this level
rightmost = node->val;
// Enqueue children (left first so right is processed last)
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
result.push_back(rightmost);
}
return result;
}
}; Time O(n) Space O(n)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
#include <vector>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
void dfs(TreeNode* root, vector<int>& res, int level){
if(!root){
return;
}
if(level==res.size()){
res.push_back(root->val);
}
dfs(root->right, res, level+1);
dfs(root->left, res, level+1);
}
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
dfs(root, res, 0);
return res;
}
};
// Time Complexity: O(N)
// Space Complexity: O(H), where H is the height of the binary tree, which is O(N) in the worst case (skewed tree) and O(log N) in the best case (balanced tree). Time O(N) Space O(H)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.