Algo-Sim › Binary Trees & BST
Binary Tree Right Side View
Medium · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Standing to the right of the tree, you see the last node of every level: a level-order BFS that keeps each level’s last node.
Problem
Given the root of a binary tree, imagine standing on its right side. Return the values of the nodes you can see, ordered from top to bottom. On each level, a node hides every node to its left.
Constraints
- 0 ≤ number of nodes ≤ 100
- -100 ≤ Node.val ≤ 100
Edge cases to test
- A left-leaning chain (visible from the right)
- Empty tree
- One node
Follow-up questions
- Is there a DFS version?
- Visit right before left and record the first node seen at each new depth: O(n) time, O(h) space.
- How do you get the left view?
- Take the first node of each level instead of the last.
Solution
Java
public List<Integer> rightSideView(TreeNode root) {
List<Integer> view = new ArrayList<>();
Queue<TreeNode> queue = new ArrayDeque<>();
if (root != null) queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (i == size - 1) view.add(node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
return view;
}Python
from collections import deque
def right_side_view(root):
view = []
queue = deque([root] if root else [])
while queue:
size = len(queue)
for i in range(size):
node = queue.popleft()
if i == size - 1:
view.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return viewC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> rightSideView(TreeNode *root) {
vector<int> view = {};
deque<TreeNode *> queue = {};
if (root != nullptr)
queue.push_back(root);
while (!queue.empty()) {
int size = int(queue.size());
for (int i = 0; i < size; i++) {
TreeNode *node = queue.front();
queue.pop_front();
if (i == size - 1)
view.push_back(node->val);
if (node->left != nullptr)
queue.push_back(node->left);
if (node->right != nullptr)
queue.push_back(node->right);
}
}
return view;
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)