Binary Tree Right Side View
2015-09-03 23:19
204 查看
https://leetcode.com/problems/binary-tree-right-side-view/
/** * 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: vector<int> rightSideView(TreeNode* root) { vector<int> res; if(root==NULL) return res; queue<TreeNode *> q1,q2; q1.push(root); while(!q1.empty()||!q2.empty()) { if(!q1.empty()) { TreeNode * temp_num=q1.front(); res.push_back(temp_num->val); while(!q1.empty()) { TreeNode * temp=q1.front(); q1.pop(); if(temp->right!=NULL) q2.push(temp->right); if(temp->left!=NULL) q2.push(temp->left); } } else { TreeNode * temp_num=q2.front(); res.push_back(temp_num->val); while(!q2.empty()) { TreeNode * temp=q2.front(); q2.pop(); if(temp->right!=NULL) q1.push(temp->right); if(temp->left!=NULL) q1.push(temp->left); } } } return res; } };
相关文章推荐
- HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP
- c++虚函数、多态性与虚表
- struts2常用常量总结
- 用IDEA建立一个web工程的步骤
- 1053. Path of Equal Weight (30)
- Effective Java 英文 第二版 读书笔记 Item 3:Enforce the singleton property with a private constructor or an enum type.
- LeetCode_invert binary tree
- DataStructure-9-排序技术
- [leetcode-115]Distinct Subsequences(java)
- Cocos 3D功能初探学习笔记(1)---摄像机
- 自学QT之判断输入内容实现按钮可用或者不可用
- sizeof在32位和64位机器上的运行结果
- 关于OCTAVE
- Intent的显式与隐式用法
- HDU 1565【状压DP】
- Meteor:延迟补偿
- 怎么改变ADS1.2的字体大小
- 练练手--求串联电阻的阻值
- nodejs 浏览器弹窗下载图片 data:image/jpeg;base64示例
- Java泛型