二叉树的层序遍历 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 ...
随机推荐
- 使用expect scp避免直接输密码
需求:现在有一百多台的机器,都是使用用户名密码登录做一些运维操作,不想因为密码变更造成管理上的一些麻烦,所以改为通过密钥文件的方式进行登录,需要提前做一些准备工作,把准备好的.SSH文件复制到各机器的 ...
- Git的使用的简单介绍
节选自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git是一个分布式的引擎, ...
- 《Inside UE4》目录
<Inside UE4>目录 InsideUE4 UE4无疑是非常优秀的世界上最顶尖的引擎之一,性能和效果都非常出众,编辑器工作流也非常的出色,更难得宝贵的是完全的开源让我们有机会去从中吸 ...
- NopCommerce 增加 Customer Attributes
预期: Customer 新增一个自定义属性 运行站点 1.Admin -> Settings -> Customer settings -> Customer form field ...
- 最小生成树 kruskal算法 codevs 1638 修复公路
1638 修复公路 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description A地区在地震过后,连接所有村庄的公 ...
- 枚举Enum使用
package com.wzy.enumt; public enum Citys { //值为静态常量,每一个常量都是一个实例 BEIJING("北京"), SHANGHAI(&q ...
- php中header函数参数的Cache-control:private,no-cache,must-revalidate,max-age是使用方法!
由于我做的是登录,也就是用户登录每次登陆时都要来访问服务器,不需要在客户机做缓存,于是在网上找了找,发下一下文章不错于是传了上来.网页的缓存是由HTTP消息头中的“Cache-control”来控制的 ...
- DbContext 和ObjectContext两者的区别
http://blog.csdn.net/lvjin110/article/details/24642911 ObjectContext是一种模型优先的开发模式,DbContext是代码优先的开发模式 ...
- 用延迟加载解决CNZZ加载慢的问题
我是不太喜欢CNZZ的, 不过既然公司要用, 还是得加, 这个公司不知道为什么不好好优化一下, 这么多功能都做了, 难道不愿意多费几分钟优化一下这个特别影响用户体验的统计代码? 现在的移动站页面, 是 ...
- nodeJS基础08:读取图片
1.读取图片 //server.js var http = require("http"); var readImage = require("./readImage&q ...