Binary Tree Right Side View 从左边视角输出二叉树的值
2015-06-01 17:43
113 查看
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].
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: //按层遍历BFS,从左到右,然后将最右边的放入 vector<int> rightSideView(TreeNode* root) { vector<int> ans; if(root==NULL) return ans; queue<TreeNode *>q; int kk; TreeNode *cur; q.push(root); while(!q.empty()) { kk=q.size(); for(int i=0;i<kk;i++) { cur=q.front(); q.pop(); if(cur->left) q.push(cur->left); if(cur->right) q.push(cur->right); } ans.push_back(cur->val);//插入最右边的值 } return ans; } };
相关文章推荐
- android有关网络连接的方法
- C++程序设计 - Week 2 类和对象初探
- Python标准库学习 (上)
- 孤儿进程与僵尸进程【比较】
- 在网页中嵌入Base64编码文件
- git指令
- 1.简介和基本术语
- Android-自动化埋点
- JavaScript变量声明那些事儿
- Repeated DNA Sequences 位操作
- C# 加密解密(DES,3DES,MD5,Base64) 类
- python 获取桌面路径
- JS中令人发指的valueOf方法介绍
- Spring的配置文件实例
- MVC与MVVM
- 新手理解 java的单立模式
- Google网络框架Volley的使用,Cache-Control=no-cache时强制缓存的处理
- 使用CArchive类进行序列化
- Contiki Timers 详解
- php获取网页内容方法总结