mediumBfsBinary TreeTrees 0 views

Binary Tree Left Side View

Return the value of the leftmost node visible at each level of a binary tree, from top to bottom.

Given the root of a binary tree, imagine yourself standing on the left side of it. Return the values of the nodes you can see, ordered from top to bottom.

At each level of the tree, the node you can see is the first one encountered when scanning that level from left to right.

Example 1

Input: root = [1,2,3,null,5,null,4]

Output: [1,2,5]

Example 2

Input: root = [1,null,3]

Output: [1,3]

Explanation: The root has no left child, so its only child (3) is the leftmost -- and only -- node visible at that level.

Example 3

Input: root = []

Output: []

Explanation: An empty tree has nothing to view.

Constraints

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

Follow-up

How would you adapt this approach to return the right side view instead?

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.