Given 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.
For example:
Given the following binary tree,
Given the following binary tree,
1 <--- / \ 2 3 <--- \ \ 5 4 <---
You should return [1, 3, 4] .
<Solution>這題可以看做 Binary Tree Level Order Traversal 的衍生題
利用同樣思路解即可,主要變動的地方是
- 每次 iteration,把 queue 裡面的最後一個 node,放到答案裡面
沒有留言:
張貼留言