LeetCode题解之Binary Tree Right Side View

1、题目描述

2、问题分析

使用层序遍历

3、代码

 1  vector<int> v;
 2     vector<int> rightSideView(TreeNode* root) {
 3         if (root == NULL)
 4             return v;
 5         
 6         queue<TreeNode*> q;
 7         q.push(root);
 8         
 9         while (!q.empty()) {
10             int size = q.size();
11             for(int i = 0; i < size; i++) {
12                 TreeNode *node = q.front();
13                 if (node->left != NULL)
14                     q.push(node->left);
15                 if (node->right != NULL)
16                     q.push(node->right);
17                     
18                 
19                 if (i == size - 1)
20                     v.push_back(node->val);
21                 q.pop();
22             }
23         }
24         
25         return v;
26         
27         
28     }
原文地址:https://www.cnblogs.com/wangxiaoyong/p/10461490.html