NeetCode #425LC-199MediumTreesNC 150NC 250
← Back to All Problems#425 · #199 · Binary Tree Right Side View(二叉树的右视图)
📌 Problem Statement & Constraints
Given the root of a binary tree, imagine standing on the right side of it and return the values of the nodes visible from top to bottom. Constraints: the number of nodes is in
[0, 100].💡 Core Algorithmic Approaches
- This is a level-order traversal where only the last node of each level is recorded.
- BFS with a frozen level size makes this trivial: the node at index
n - 1is the rightmost. - A DFS that visits the right child first and records the first node seen at each new depth is an equally valid approach.
- Both are O(n).
💻 Benchmark Python3 Implementation
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
from collections import deque
if not root:
return []
res = []
q = deque([root])
while q:
n = len(q)
for i in range(n):
node = q.popleft()
if i == n - 1: # the rightmost node of this level
res.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
return res
# DFS variant: right child first, record the first node at each new depth
class Solution2:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
res = []
def dfs(node: Optional[TreeNode], depth: int) -> None:
if not node:
return
if depth == len(res): # first node seen at this depth
res.append(node.val)
dfs(node.right, depth + 1) # right first -> rightmost wins
dfs(node.left, depth + 1)
dfs(root, 0)
return res⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): every node is visited once.
💾 Space Complexity
O(w) for the BFS queue, or O(h) for the DFS recursion stack.
⚠️ Interview Pitfalls & Follow-ups
- Taking the first node of each level in BFS: that is the left view; the right view needs the last node.
- Visiting the left child first in the DFS variant: the leftmost node would be recorded instead.
- Using
depth == len(res)as the condition: this works precisely because the right child is explored first. - Forgetting the empty-tree case: an empty tree has an empty view.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Trees Problems
中文速览与面试要点
二叉树的右视图
给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。约束:二叉树的节点个数的范围是 [0, 100];-100 ≤ Node.val ≤ 100。
核心要点
- BFS 取每层最后一个(标准解) :层序遍历时,每层的最后一个节点即为该层从右侧能看到的节点。
- 实现细节 :用 size 固定当前层节点数,循环到最后一个时记录它的值。
- DFS 方案 :优先递归右子树,并记录「已访问的最大深度」——若当前深度首次被访问,则该节点是该层最右侧的可见节点。
- 复杂度 :O(n) 时间、O(w) 空间。
常见陷阱
- 误以为「右视图」就是「一直走右子节点」 :若右子树较浅,左子树的某些节点也会露出来。必须逐层取最右。
- BFS 时记录每层第一个 :那是左视图。
- DFS 时优先递归左子树 :会先记录左侧节点,导致结果错误。必须优先右子树。
参考实现
from collections import deque
class Solution:
def rightSideView(self, root: 'TreeNode') -> list[int]:
if not root:
return []
res = []
q = deque([root])
while q:
size = len(q)
for i in range(size):
node = q.popleft()
if i == size - 1: # 本层最后一个
res.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
return res
# DFS 方案(优先右子树)
# def rightSideView(self, root):
# res = []
# def dfs(node, d):
# if not node: return
# if d == len(res): res.append(node.val) # 该层首次访问
# dfs(node.right, d + 1)
# dfs(node.left, d + 1)
# dfs(root, 0)
# return res