leetcode_199题——Binary Tree Right Side View(广度优先搜索,队列queue)

#include<iostream>
#include<queue>
#include<vector>
using namespace std;

// Definition for binary tree
 struct TreeNode {
	 int val;
	 TreeNode *left;
	 TreeNode *right;
	 TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 };

 /*这道题采用广度优先搜索的算法来做,一层一层的进行搜索,而在每一层将这一层所有的
 结点加入到队列里面去,并记录下结点的个数,这些结点是从左到右依次加入了,当从队列出来这一层
 队列的最后一个元素时,就是最右边的那个数。
 */
vector<int> rightSideView(TreeNode *root) {
	queue<TreeNode*> temp;//用来计算的队列
	int row_size=1;
	TreeNode* temp_node;
	vector<int> result_last;
	if(root==NULL)
		return result_last;
	temp.push(root);

	while(!temp.empty())
	{
		while(row_size--)
		{
			temp_node=temp.front();
			temp.pop();
			if(row_size==0)
				result_last.push_back(temp_node->val);
			if(temp_node->left!=NULL)
				temp.push(temp_node->left);
			if(temp_node->right!=NULL)
				temp.push(temp_node->right);
		}
		row_size=temp.size();
	}
	return result_last;
 }

int main()
{

}

  

时间: 2024-10-22 02:34:49

leetcode_199题——Binary Tree Right Side View(广度优先搜索,队列queue)的相关文章

【Leetcode】【Medium】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].

【LeetCode】199. Binary Tree Right Side View

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 <--- Y

LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

199. 二叉树的右视图 199. 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. LeetCode19

leetcode_103题——Binary Tree Zigzag Level Order Traversal(广度优先搜索,队列queue,栈stack)

Binary Tree Zigzag Level Order Traversal Total Accepted: 31183 Total Submissions: 117840My Submissions Question Solution Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left fo

leetcode_107题——Binary Tree Level Order Traversal II (二叉树,广度优先搜索,队列,栈)

Binary Tree Level Order Traversal II Total Accepted: 37829 Total Submissions: 122499My Submissions Question Solution Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from le

leetcode_102题——Binary Tree Level Order Traversal(二叉树,广度优先搜索,队列)

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] confus

&lt;LeetCode OJ&gt; 199. Binary Tree Right Side View

Total Accepted: 40438 Total Submissions: 117654 Difficulty: Medium 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

199. Binary Tree Right Side View java solutions

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].

Leetcode 随笔之 ------ Binary Tree Right Side View

先等价转一下题意为 “从上至下保存树的每一层的最右节点” 那么我的大致思路可以归纳为: 1. DFS遍历树,把节点和对应的层数标记在一个HashMap里 2. BFS对整棵树构建一个完整的队列, 这个队列的特点就是按一层一层连接起来的 3. 从头遍历队列,根据Hashmap里的层数标记找出每层的最右节点,具体方法是维护一个pre变量保存之前的节点,如果当前节点值与之不等,则把pre加入结果集,另外队尾也要加入结果集 Ps: 本来第二步也打算用iterator遍历结果出了异常,发现是在使用iter