Binary Tree Right Side View

Medium

Problem: Binary Tree Right Side View

Problem Statement

Given the root of a binary tree, return the values of the nodes at each level starting from the right, in the order they occur at that level. The right side view of a binary tree is a list of elements that would be observed by standing at the rightmost position and looking at the leftmost elements at each level.

Rules and Constraints

  • The input root node is a binary tree where each node has a value (non-negative integer), a left child node, and a right child node.
  • You should not change the structure of the binary tree.
  • The time complexity of your algorithm should be O(n), where n is the number of nodes in the binary tree.
  • The space complexity of your algorithm should be O(h), where h is the height of the binary tree (the maximum number of nodes at each level).

Note that this problem does not require handling nodes with null or undefined values.

Example

Input: {"input_data":[1,2,3]} Output: [1,2,3]

CompaniesGoogleMetaAmazon
JavaScript

Login to write code

Solve problems, verify your skills, and earn XP.