Link
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,
1 2 3 4 5
|
``1 <--- / 2 3 <--- ` ``5 4 <---
|
You should return [1, 3, 4].
2. Analysis
3. Solution(s)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class (object): def rightSideView(self, root): """ :type root: TreeNode :rtype: List[int] """ if not root: return list() res, level = list(), [root] while level: res.append(level[-1].val) tmp = list() for node in level: tmp.append(node.left) tmp.append(node.right) level = [x for x in tmp if x] return res
|
近期评论