bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 199. Binary Tree Right Side View

199. Binary Tree Right Side View

Leetcode Solutions

BFS: One Queue + Level Size Measurements

  1. Initialize an empty list rightside to store the visible nodes.
  2. Initialize a queue and add the root node to it.
  3. While the queue is not empty: a. Record the number of nodes in the current level (levelLength). b. Iterate over all nodes in the current level. c. For each node, remove it from the queue. d. If it's the last node in the current level, add its value to rightside. e. Add the node's left and right children to the queue if they exist.
  4. Return the list rightside.
UML Thumbnail

Recursive DFS

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...