Right Side View of Binary Tree in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to find the right side view of a binary tree changes as the tree grows.
How does the number of nodes affect the work done to see the tree from the right side?
Analyze the time complexity of the following code snippet.
function rightSideView(root: TreeNode | null): number[] {
if (!root) return [];
const queue: TreeNode[] = [root];
const result: number[] = [];
while (queue.length > 0) {
const levelSize = queue.length;
for (let i = 0; i < levelSize; i++) {
const node = queue.shift()!;
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
if (i === levelSize - 1) result.push(node.val);
}
}
return result;
}
This code visits each node level by level and records the last node's value at each level to get the right side view.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs once per tree level, and the inner for loop visits every node in that level.
- How many times: Each node is visited exactly once during the entire traversal.
As the number of nodes increases, the code visits each node once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The operations grow linearly with the number of nodes.
Time Complexity: O(n)
This means the time to find the right side view grows in direct proportion to the number of nodes in the tree.
[X] Wrong: "The time depends on the height of the tree only because we process level by level."
[OK] Correct: Even though we process level by level, every node is visited once, so the total time depends on the total number of nodes, not just the height.
Understanding this helps you explain how breadth-first search works and how to analyze tree traversals, a common skill in coding interviews.
What if we used a depth-first search instead of breadth-first search? How would the time complexity change?
Practice
Solution
Step 1: Understand the view perspective
The right side view shows nodes visible when looking at the tree from the right side.Step 2: Identify visible nodes
Only the rightmost node at each level is visible from the right side.Final Answer:
Nodes visible when the tree is viewed from the right side -> Option CQuick Check:
Right side view = rightmost nodes [OK]
- Confusing right side view with left side view
- Thinking it shows all leaf nodes
- Assuming it shows deepest level nodes only
Solution
Step 1: Check class definition syntax
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } uses TypeScript class with proper types and constructor.Step 2: Compare other options
function TreeNode(val) { this.val = val; this.left = null; this.right = null; } is JavaScript function, not TypeScript class. interface TreeNode { val: number; left: TreeNode; right: TreeNode; } misses nullability. type TreeNode = { val: number; left?: TreeNode; right?: TreeNode } uses type but lacks constructor.Final Answer:
class TreeNode with constructor and nullable children -> Option DQuick Check:
TypeScript class with constructor = class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } [OK]
- Using JavaScript function instead of TypeScript class
- Not allowing null for left/right children
- Using interface without constructor
1
/ \
2 3
\ \
5 4
function rightSideView(root: TreeNode | null): number[] {
const result: number[] = [];
if (!root) return result;
const queue: (TreeNode | null)[] = [root];
while (queue.length) {
const size = queue.length;
for (let i = 0; i < size; i++) {
const node = queue.shift()!;
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
if (i === size - 1) result.push(node.val);
}
}
return result;
}Solution
Step 1: Traverse level by level
Level 1: node 1; Level 2: nodes 2, 3; Level 3: nodes 5, 4.Step 2: Pick rightmost node at each level
Level 1 rightmost: 1; Level 2 rightmost: 3; Level 3 rightmost: 4.Final Answer:
[1, 3, 4] -> Option AQuick Check:
Rightmost nodes per level = [1, 3, 4] [OK]
- Choosing leftmost nodes instead of rightmost
- Mixing node values from different levels
- Ignoring null children in traversal
function rightSideView(root: TreeNode | null): number[] {
const result: number[] = [];
if (!root) return result;
const queue: TreeNode[] = [root];
while (queue.length) {
const size = queue.length;
for (let i = 0; i < size; i++) {
const node = queue.shift();
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
if (i === size - 1) result.push(node.val);
}
}
return result;
}Solution
Step 1: Analyze queue.shift() return type
queue.shift() can return undefined if queue is empty, but here size ensures it's not empty. Still, TypeScript requires checking.Step 2: Fix type safety
Change queue type to allow undefined and check node is not undefined before accessing properties.Final Answer:
Change queue type and check node after shift -> Option BQuick Check:
Type safety requires checking shift result [OK]
- Ignoring possible undefined from shift()
- Removing correct condition for rightmost node
- Changing queue initialization incorrectly
[1, 2, null, 5, null, null, 3, null, 4]. How can you adapt your approach to get the right side view?Solution
Step 1: Understand preorder with nulls
The array represents tree nodes with nulls marking missing children, so tree structure is implicit.Step 2: Rebuild tree then apply right side view
Reconstruct the binary tree from this array, then run BFS to get right side view nodes.Final Answer:
Rebuild tree from preorder with nulls, then BFS -> Option AQuick Check:
Need tree structure to find right side view [OK]
- Assuming preorder array directly gives right side view
- Ignoring nulls and tree structure
- Trying to pick nodes without tree reconstruction
Ready to prove it?
Pass the test and this pattern moves unknown
