Binary Tree Right Side View

思路::主要用到BFS,把最后边的一个元素存储。

class Solution {
public:
   vector<int> rightSideView(TreeNode* root) 
		{
			vector<int> Result;
			if (root==NULL)
			{
				return Result;
			}
			//BFS
			queue<TreeNode* > q1;
			q1.push(root);
			int cnt=1;
			int level;
			while (!q1.empty())
			{
				level=0;
				for (int i=0;i<cnt;i++)
				{
					TreeNode * tmp=q1.front();
					if (i==cnt-1)
					{
						Result.push_back(tmp->val);
					}
				   q1.pop();
				   if (tmp->left)
				   {
					   q1.push(tmp->left);
					   level++;
				   }
				   if (tmp->right)
				   {
					   q1.push(tmp->right);
					   level++;
				   }
				}
				cnt=level;
			}
			return Result;
		}
};


原文地址:https://www.cnblogs.com/liguangsunls/p/7006941.html