Problem Description

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

Examples

Example 1:

https://assets.leetcode.com/uploads/2021/02/14/tree.jpg

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

Example 2:

Input: root = [1,null,3]
Output: [1,3]

Example 3:

Input: root = []
Output: []

Constraints:

解析

題目給了一個二元樹根結點 root

想要找到二元樹,每個level 最右邊的結點如下圖

tree-right-side-1.png

直覺的作法是透過 Breadth First Traversal 廣度優先演算法實作

廣度優先演算法透過一個 queue ,每次把同一層的 node 照順序(由左而右)放到 queue 內

然後這時最右邊的結點就是我們要尋找的結點了