• 39.5%

https://leetcode.com/problems/binary-tree-right-side-view/#/description

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.

o(N)时间 o(N)空间

https://discuss.leetcode.com/topic/11310/my-c-solution-modified-preorder-traversal

My C++ solution, modified preorder traversal

https://discuss.leetcode.com/topic/23030/simple-c-solution-btw-i-like-clean-codes

Simple C++ solution (BTW: I like clean codes)

https://discuss.leetcode.com/topic/11303/9ms-c-bfs-o-n-time-concise-with-explanation

9ms C++ BFS, O(n) time, concise with explanation

9ms C++ iterative, concise code with explanation

Using a queue mQ to perform level order traversal. In the beginning of a level traversal, the last element is pushed into result array ret. The core idea is similar with Binary Tree Level Order Traversal

O(n) time, O(logn) space

https://discuss.leetcode.com/topic/16164/5-9-lines-python-48-ms

5-9 Lines Python, 48+ ms

Solution 1: Recursive, combine right and left: 5 lines, 56 ms

Compute the right view of both right and left left subtree, then combine them. For very unbalanced trees, this can be O(n^2), though.

Solution 2: Recursive, first come first serve: 9 lines, 48 ms

DFS-traverse the tree right-to-left, add values to the view whenever we first reach a new record depth. This is O(n).

Solution 3: Iterative, level-by-level: 7 lines, 48 ms

Traverse the tree level by level and add the last value of each level to the view. This is O(n).

1ms, 82.64%, July 14th, 2016

https://discuss.leetcode.com/topic/11768/my-simple-accepted-solution-java

My simple accepted solution(JAVA)

The core idea of this algorithm:

1. Each depth of the tree only select one node.

2. View depth is current size of result list.

Here is the code: