199. Binary Tree Right Side View
Binary Tree Right Side View (LeetCode 199) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Level Order Traversal subpattern.
How to solve Binary Tree Right Side View
Binary Tree Right Side View is a Level Order Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master level order traversal using BFS. Solve zigzag, right side view, and level sum problems.
Work towards the solution in these steps:
- Consider which tree traversal method allows you to process nodes level by level, such as Breadth-First Search (BFS).
- Think about how traversing each level of the tree can help identify the rightmost node visible from that level.
- Using a queue can facilitate level order traversal; try to process nodes level by level and keep track of their order.
The reference solution runs in O(N) time and O(W) space.
The Binary Tree Right Side View solution and editorial walks through the full approach with reference code.
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 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
Topics and companies
Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree.
Reported in interviews at Facebook, Amazon, ByteDance, Bloomberg, Microsoft.
How to practise Binary Tree Right Side View on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N) time and O(W) space.
Open Binary Tree Right Side View in the code editor, or read the Binary Tree Right Side View editorial for a worked solution with its approach and complexity analysis.
Where Binary Tree Right Side View sits in the DSA pattern sheet
- Tree Traversal Patterns (DFS & BFS) pattern guide
- Level Order Traversal subpattern
- The full DSA patterns sheet
Problems related to Binary Tree Right Side View
Other problems that use the same Level Order Traversal and Tree Traversal Patterns (DFS & BFS) techniques:
- Binary Tree Level Order Traversal — Medium, same subpattern
- Binary Tree Zigzag Level Order Traversal — Medium, same subpattern
- Find Largest Value in Each Tree Row — Medium, same subpattern
- Maximum Level Sum of a Binary Tree — Medium, same subpattern
- All Nodes Distance K in Binary Tree — Medium, same pattern
- Balanced Binary Tree — Easy, same pattern
Binary Tree Right Side View FAQ
What is the LeetCode number of Binary Tree Right Side View?
Binary Tree Right Side View is LeetCode problem 199, rated Medium.
What pattern is used to solve Binary Tree Right Side View?
Binary Tree Right Side View is a Level Order Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Level Order Traversal subpattern.
What is the time complexity of Binary Tree Right Side View?
The reference solution runs in O(N) time and O(W) space.
Which companies ask Binary Tree Right Side View?
Binary Tree Right Side View has been reported in interviews at Facebook, Amazon, ByteDance, Bloomberg, Microsoft.