二叉树的层序遍历 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 ...
随机推荐
- Ubuntu搭建Note.Js 平台
1. 安装nodeJs和npm apt-get install nodejsapt-get install npm 2 .node有一个模块叫n,是专门用来管理node.js的版本的.首先安装n模块: ...
- ubuntu下安装lrzsz
secureCRT中可以使用rz和sz命令上传和下载文件,可是这要linux中安装了lrzsz才可以.我用的时候无法使用apt-get自动安装,下面介绍手动安装的方法. 1 下载lrzsz软件 ht ...
- gcc/g++中weak弱符号及alias别名
最近查看linux内核代码时,表现了一些编译器选项如__attribute_((weak)).__attribute__( (alias("target"))),一开始不了解,后来 ...
- linux init 启动顺序
redhat init大致启动过程 第一个运行的程序是/sbin/init,该文件会读取/etc/inittab文件,并依据此文件来进行初始化工作.比如在设定了运行等级 “:id:3:initdefa ...
- 《Paxos Made Simple》翻译
1 Introduction 可能是因为之前的描述对大多数读者来说太过Greek了,Paxos作为一种实现容错的分布式系统的算法被认为是难以理解的.但事实上,它可能是最简单,最显而易见的分布式算法了. ...
- Android(Linux)线路规程的使用
一般来说,车载导航主机都需要外接若干个UART的外设,如支持HFP的蓝牙模块.与原车通信的CAN解码盒模块.u-blox的GPS模块和DVD机芯等.早年使用Telechips TCC8902+ ...
- 发一份shiro标准配置,特此记录
主要还是整合了本地ehcache,集群session管理过段时间放出 <?xml version="1.0" encoding="UTF-8"?> ...
- winform窗体控件(全)
回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...
- python基础之循环结构以及列表
python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.python IDE的选择 IDE的全称叫做集成 ...
- javascript详解系列-函数表达式
1.递归 function fact(num){ if(num<1){ return 1; } else{ return num*fact(num-1); } } var author = fa ...