Right Side View of Binary Tree in DSA Javascript - 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) {
if (!root) return [];
const queue = [root];
const result = [];
while (queue.length > 0) {
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;
}
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 level, and inside it, the for loop visits every node at that level.
- How many times: Every node in the tree is visited exactly once during the for loops combined.
As the number of nodes (n) grows, the code visits each node once, so the total operations grow roughly in direct proportion to n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The work grows linearly with the number of nodes.
Time Complexity: O(n)
This means the time to find the right side view grows directly with the number of nodes in the tree.
[X] Wrong: "Since we only take one node per level, the time complexity is O(height) or O(log n)."
[OK] Correct: Even though we record one node per level, we still visit every node to know which one is last at each level, so the time depends on all nodes, not just the height.
Understanding this helps you explain how level-by-level tree traversal works and why visiting all nodes is necessary, a common pattern in tree problems.
"What if we used a depth-first search instead of breadth-first search? How would the time complexity change?"
Practice
Right Side View of a binary tree represent?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:
The nodes visible when the tree is viewed from the right side -> Option BQuick Check:
Right side view = rightmost nodes at each level [OK]
- Confusing right side view with left side view
- Thinking it shows all leaf nodes
- Assuming it shows deepest nodes only
Solution
Step 1: Check typical function signature
The function usually takes the root node as input to start traversal.Step 2: Validate options
function rightSideView(root) { } correctly takes only the root node, which is standard for this problem.Final Answer:
function rightSideView(root) { } -> Option CQuick Check:
Function takes root node only [OK]
- Adding unnecessary parameters
- Omitting root parameter
- Using unclear parameter names
class TreeNode {
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
const root = new TreeNode(1,
new TreeNode(2, null, new TreeNode(5)),
new TreeNode(3, null, new TreeNode(4))
);
function rightSideView(root) {
const result = [];
const queue = [root];
while (queue.length) {
const size = queue.length;
for (let i = 0; i < size; i++) {
const node = queue.shift();
if (i === size - 1) result.push(node.val);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
}
return result;
}
console.log(rightSideView(root));Solution
Step 1: Traverse level by level and identify rightmost nodes
Level 1: Node 1 (only node), Level 2: Nodes 2 and 3 (rightmost is 3), Level 3: Nodes 5 and 4 (rightmost is 4).Step 2: Collect rightmost nodes in order
Result array is [1, 3, 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
- Forgetting to check all nodes at a level
function rightSideView(root) {
const result = [];
const queue = [root];
while (queue.length) {
const size = queue.length;
for (let i = 0; i < size; i++) {
const node = queue.shift();
if (i === 0) result.push(node.val);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
}
return result;
}Solution
Step 1: Analyze the condition for pushing node values
The code pushes node.val when i === 0, which is the first node at each level (leftmost).Step 2: Understand right side view requirement
Right side view requires the rightmost node at each level, which is when i === size - 1.Final Answer:
It pushes the leftmost node instead of the rightmost node at each level -> Option AQuick Check:
Right side view needs last node per level, not first [OK]
- Using i === 0 instead of i === size - 1
- Ignoring null root check
- Confusing order of child insertion
1 / 2 / 3What is the right side view output for this tree?
Solution
Step 1: Visualize the tree and right side view
Since all nodes are on the left, the right side view shows these nodes as they are the only visible ones.Step 2: List nodes level-wise
Level 1: 1, Level 2: 2, Level 3: 3. All are visible from right side as no right nodes block view.Final Answer:
[1, 2, 3] -> Option DQuick Check:
Right side view includes all nodes when only left children exist [OK]
- Assuming right side view is empty if no right children
- Skipping left children in right side view
- Confusing right side view with right subtree nodes only
Ready to prove it?
Pass the test and this pattern moves unknown
