二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容。

问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行。

#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的更多相关文章

  1. 五三想休息,今天还学习,图解二叉树的层序遍历BFS(广度优先)模板,附面试题题解

    壹 ❀ 引 我在从JS执行栈角度图解递归以及二叉树的前.中.后遍历的底层差异一文中,从一个最基本的数组遍历引出递归,在掌握递归的书写规则后,又从JS执行栈角度解释了二叉树三种深度优先(前序.中序后序) ...

  2. LeetCode 102. 二叉树的层序遍历 | Python

    102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...

  3. 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, ...

  4. 剑指offer 二叉树的层序遍历

    剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...

  5. leetcode之二叉树的层序遍历

    1.题目描述 2.题目分析 二叉树的层序遍历主要算法思想是使用 队列这一数据结构实现,这个数据结构多应用在和 图相关的算法.例如图的广度优先遍历就可以使用队列的方法实现.本题的关键在于如何识别出一层已 ...

  6. 刷题-力扣-107. 二叉树的层序遍历 II

    107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...

  7. 笔试算法题(37):二叉树的层序遍历 & 最长递增的数字串

    出题:要求层序遍历二叉树,从上到下的层次,每一层访问顺序为从左到右,并将节点一次编号,输出如下:如果只要求打印指定的level的节点,应该如何实现. a b  c d  e  f  g h  i  分 ...

  8. leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  9. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

随机推荐

  1. Xstream学习资料

    java中有关xml操作的,我们项目中首推Xstream.至于原因不说了.跟着大众的脚步走应该没错的.有关Xstream的文档如下. 官方文档 XStream完美转换XML.JSON XStream实 ...

  2. Java 6 JVM参数选项大全(中文版)

    原文来自: http://kenwublog.com/docs/java6-jvm-options-chinese-edition.htm 本文是基于最新的SUN官方文档Java SE 6 Hotsp ...

  3. [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板

      引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate)   切换模板的两种方式:   使用DataTemplateSelecto ...

  4. 帆软报表FineReport SQLServer数据库连接失败常见解决方案

    1. 问题描述 帆软报表FineReport客户端连接SQLServer(2000.2005等),常常会出现如下错误:com.microsoft.sqlserver.jdbc.SQLServerExc ...

  5. Java中的static关键字解析

    Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...

  6. BZOJ 3343: 教主的魔法 [分块]【学习笔记】

    3343: 教主的魔法 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1172  Solved: 526[Submit][Status][Discus ...

  7. Vijos1881闪烁的繁星 [线段树]

    P1881闪烁的繁星  背景 繁星闪烁着--深蓝的太空何曾听得见他们对语沉默中微光里他们深深的互相颂赞了 描述 繁星, 漫天的繁星.繁星排成一列, 我数一数呀, 一共有N只小星星呢. 星星们是听话的好 ...

  8. 第27章 java I/O输入输出流

    java I/O输入输出流 1.编码问题 import java.io.UnsupportedEncodingException; /** * java涉及的编码 */ public class En ...

  9. 重写Oracle的wm_concat函数,自定义分隔符、排序

    oracle中,wm_concat函数是一个聚合函数,和mysql中的group_concat函数类似,不过group_concat函数比较强大,可以定义分隔符和排序,当然所谓强大是相对的,这里假使我 ...

  10. LCM 轻量级通信组件

    LCM和ZMQ比较 http://www.doc88.com/p-6711552253536.html 基于LCM和ZeroMQ的进程间通信研究 2.简介 LCM(Lightweight Commuc ...