二叉树的层序遍历 BFS
二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容。
问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行。
#include <iostream>
#include <vector>
#include <deque>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdlib> using namespace std; typedef struct BinTree{
int data;
struct BinTree *left;
struct BinTree *right;
}BinTree; /* 按照层序遍历方法遍历二叉树,使用一个队列来辅助 */
void BreadthFirst(BinTree *root){
if(root == NULL)
return;
deque<BinTree *> q;
q.push_back(root); BinTree *p;
while(!q.empty()){
p = q.front();
q.pop_front(); cout<<p->data; if(p->left)
q.push_back(p->left); if(p->right)
q.push_back(p->right);
}
} //测试
int main()
{
/* 创建以下的树
10
/ \
8 2
/ \ /
3 5 2
*/
struct node *root = newNode(10);
root->left = newNode(8);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(5);
root->right->left = newNode(2);
iterativePreorder(root);
return 0;
}
问题二: 按层输出二叉树,每一层单独输出为一行。
#include <vector>
#include <deque>
#include <queue> using namespace std; 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> ans;
if(root == nullptr) return ans;
queue<TreeNode* > que;
que.push(root); TreeNode* curr;
while(!que.empty()) {
int cnt = que.size();
for(int i = 0; i < cnt; i++) {
curr = que.front();
que.pop();
if(curr->left) {
que.push(curr->left);
}
if(curr->right) {
que.push(curr->right);
}
cout << curr->val << " ";
}
ans.push_back(curr->val);
cout << endl;
}
return ans;
}
}; int main() {
/* 创建以下的树
10
/ \
8 2
/ \ /
3 5 2
*/ Solution sln; TreeNode *root = new TreeNode(10);
root->left = new TreeNode(8);
root->right = new TreeNode(2);
root->left->left = new TreeNode(3);
root->left->right = new TreeNode(5);
root->right->left = new TreeNode(2);
sln.rightSideView(root); return 0;
}
LeetCode: 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].
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
if(root == NULL) return result;
deque<TreeNode *> q;
q.push_back(root);
TreeNode *current;
while(!q.empty()) {
int len = q.size();
for(int i = 0; i < len; i++) {
current = q.front();
q.pop_front();
if(current->left) {
q.push_back(current->left);
}
if(current->right) {
q.push_back(current->right);
}
}
result.push_back(current->val);
}
return result;
}
};
同理:可以得到二叉树的左视图求解方式。
vector<int> leftSideView(TreeNode *root)
{
vector<int> ans;
if(root == NULL) return ans;
queue<TreeNode* > que;
que.push(root); TreeNode* curr;
while(!que.empty()) {
int cnt = que.size(); for(int i = 0; i < cnt; i++) {
curr = que.front();
if( i == 0){
ans.push_back(curr->val);
}
que.pop(); if(curr->left) {
que.push(curr->left);
}
if(curr->right) {
que.push(curr->right);
}
}
}
return ans;
}
二叉树的层序遍历 BFS的更多相关文章
- 五三想休息,今天还学习,图解二叉树的层序遍历BFS(广度优先)模板,附面试题题解
壹 ❀ 引 我在从JS执行栈角度图解递归以及二叉树的前.中.后遍历的底层差异一文中,从一个最基本的数组遍历引出递归,在掌握递归的书写规则后,又从JS执行栈角度解释了二叉树三种深度优先(前序.中序后序) ...
- LeetCode 102. 二叉树的层序遍历 | Python
102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...
- 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, ...
- 剑指offer 二叉树的层序遍历
剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...
- leetcode之二叉树的层序遍历
1.题目描述 2.题目分析 二叉树的层序遍历主要算法思想是使用 队列这一数据结构实现,这个数据结构多应用在和 图相关的算法.例如图的广度优先遍历就可以使用队列的方法实现.本题的关键在于如何识别出一层已 ...
- 刷题-力扣-107. 二叉树的层序遍历 II
107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...
- 笔试算法题(37):二叉树的层序遍历 & 最长递增的数字串
出题:要求层序遍历二叉树,从上到下的层次,每一层访问顺序为从左到右,并将节点一次编号,输出如下:如果只要求打印指定的level的节点,应该如何实现. a b c d e f g h i 分 ...
- leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
随机推荐
- Linux可插拔认证模块(PAM)的配置文件、工作原理与流程
PAM的配置文件: 我们注意到,配置文件也放在了在应用接口层中,他与PAM API配合使用,从而达到了在应用中灵活插入所需鉴别模块的目的.他的作用主要是为应用选定具体的鉴别模块,模块间的组合以及规定模 ...
- linux basic commands
1. man - an interface to the on-line reference manuals $man man 2. apt - advanced package tool SEE A ...
- 【转载】Linux中常用操作命令
说明:开始学习linux系统,为了方便查看,特转载一篇Linux中常用操作命令,转载地址:http://www.cnblogs.com/laov/p/3541414.html 正文: Linux简介及 ...
- 字符串切分 String.Split 和 Regex.Split
当切割字符串的是单个字符时可使用String.Split string strSample="ProductID:20150215,Categroy:Food,Price:15.00&quo ...
- POJ1740A New Stone Game[组合游戏]
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5769 Accepted: 3158 ...
- Vc6.0头文件的定义
Vc6.0头文件的定义 #ifndef __HEADER__ #define __HEADER__ int fun(int i); #endif
- AC日记——寻找道路 洛谷 P2296
题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...
- HTML标记语言篇--学习笔记01
HTML标记语言篇 第1章 HTML基础 1.1 基本概念 WWW 是"World Wide Web"(全球广域网)的缩写,简称为Web,中文又称为"万维网" ...
- 【转载】WEB前端开发规范文档
本文转载自谈笑涧<WEB前端开发规范文档> 为 新项目写的一份规范文档, 分享给大家. 我想前端开发过程中, 无论是团队开发, 还是单兵做站, 有一份开发文档做规范, 对开发工作都是很有益 ...
- AppBox升级进行时 - 如何向OrderBy传递字符串参数(Entity Framework)
AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. Entity Framework提供的排序功能 再来回顾一下上篇文章,加载用户 ...