199. Binary Tree Right Side View LeetCode Solution
In this guide, you will get 199. Binary Tree Right Side View LeetCode Solution with the best time and space complexity. The solution to Binary Tree Right Side View problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Binary Tree Right Side View solution in C++
- Binary Tree Right Side View solution in Java
- Binary Tree Right Side View solution in Python
- Additional Resources

Problem Statement of Binary Tree Right Side View
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 3:
Input: root = [1,null,3]
Output: [1,3]
Example 4:
Input: root = []
Output: []
Constraints:
The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
199. Binary Tree Right Side View LeetCode Solution in C++
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
if (root == nullptr)
return {};
vector<int> ans;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
const int size = q.size();
for (int i = 0; i < size; ++i) {
TreeNode* node = q.front();
q.pop();
if (i == size - 1)
ans.push_back(node->val);
if (node->left)
q.push(node->left);
if (node->right)
q.push(node->right);
}
}
return ans;
}
};
/* code provided by PROGIEZ */
199. Binary Tree Right Side View LeetCode Solution in Java
class Solution {
public List<Integer> rightSideView(TreeNode root) {
if (root == null)
return new ArrayList<>();
List<Integer> ans = new ArrayList<>();
Queue<TreeNode> q = new ArrayDeque<>(List.of(root));
while (!q.isEmpty()) {
final int size = q.size();
for (int i = 0; i < size; ++i) {
TreeNode node = q.poll();
if (i == size - 1)
ans.add(node.val);
if (node.left != null)
q.offer(node.left);
if (node.right != null)
q.offer(node.right);
}
}
return ans;
}
}
// code provided by PROGIEZ
199. Binary Tree Right Side View LeetCode Solution in Python
class Solution:
def rightSideView(self, root: TreeNode | None) -> list[int]:
if not root:
return []
ans = []
q = collections.deque([root])
while q:
size = len(q)
for i in range(size):
root = q.popleft()
if i == size - 1:
ans.append(root.val)
if root.left:
q.append(root.left)
if root.right:
q.append(root.right)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.