首页 > 其他 > 详细

LeetCode题解之Binary Tree Right Side View

时间:2019-03-02 16:18:23      阅读:141      评论:0      收藏:0      [点我收藏+]

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     }

 

LeetCode题解之Binary Tree Right Side View

原文:https://www.cnblogs.com/wangxiaoyong/p/10461490.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!