LeetCode 199. Binary Tree Right Side View

原题链接在这里:https://leetcode.com/problems/binary-tree-right-side-view/

题目:

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         <---
 \     \
  5     4       <---

You should return [1, 3, 4]

题解:

Binary Tree Level Order Traversal相似,通过BFS一层一层扫树,这里是仅添加最右面的一个点,就是size is decreased to 0 的时候.

Time Complexity: O(n). n是tree的node总数.

Space: O(n), que的大小.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     public List<Integer> rightSideView(TreeNode root) {
18         List<Integer> res = new ArrayList<>();
19         if(root == null){
20             return res;
21         }
22 
23         LinkedList<TreeNode> que = new LinkedList<>();
24         que.add(root);
25         while(!que.isEmpty()){
26             int size = que.size();
27             while(size-- > 0){
28                 TreeNode cur = que.poll();
29                 if(cur.left != null){
30                     que.add(cur.left);
31                 }
32 
33                 if(cur.right != null){
34                     que.add(cur.right);
35                 }
36 
37                 if(size == 0){
38                     res.add(cur.val);
39                 }
40             }
41         }
42 
43         return res;
44     }
45 }

AC C++:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 8  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 9  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10  * };
11  */
12 class Solution {
13 public:
14     vector<int> rightSideView(TreeNode* root) {
15         vector<int> res;
16         if(!root){
17             return res;
18         }
19 
20         queue<TreeNode*> que;
21         que.push(root);
22         while(!que.empty()){
23             int size = que.size();
24             while(size-- > 0){
25                 TreeNode* cur = que.front();
26                 que.pop();
27 
28                 if(cur->left){
29                     que.push(cur->left);
30                 }
31 
32                 if(cur->right){
33                     que.push(cur->right);
34                 }
35 
36                 if(size == 0){
37                     res.push_back(cur->val);
38                 }
39             }
40         }
41 
42         return res;
43     }
44 };

AC Python:

 1 # Definition for a binary tree node.
 2 # class TreeNode:
 3 #     def __init__(self, val=0, left=None, right=None):
 4 #         self.val = val
 5 #         self.left = left
 6 #         self.right = right
 7 class Solution:
 8     def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
 9         if not root:
10             return []
11         
12         res = []
13         que = deque([root])
14 
15         while que:
16             size = len(que)
17             for i in range(size):
18                 cur = que.popleft()
19 
20                 if cur.left:
21                     que.append(cur.left)
22 
23                 if cur.right:
24                     que.append(cur.right)
25                 
26                 if i == size - 1:
27                     res.append(cur.val)
28         
29         return res
30         

 

posted @ 2015-09-10 02:16  Dylan_Java_NYC  阅读(265)  评论(0编辑  收藏  举报