标签:
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].
1 class Solution { 2 public: 3 vector<int> rightSideView(TreeNode* root) { 4 vector<int> ret; 5 if(root==NULL) return ret; 6 7 queue<TreeNode*> Q; 8 Q.push(root); 9 TreeNode * node; 10 while(!Q.empty()){ 11 int qlen=Q.size(); 12 for(int i=0;i<qlen;i++){ 13 node=Q.front(); 14 Q.pop(); 15 16 if(node->left) Q.push(node->left); 17 if(node->right) Q.push(node->right); 18 19 } 20 ret.push_back(node->val); 21 } 22 23 return ret; 24 } 25 };
【leetcode】Binary Tree Right Side View
标签:
原文地址:http://www.cnblogs.com/jawiezhu/p/4521688.html