当前位置:网站首页>二叉树的层序遍历
二叉树的层序遍历
2022-06-21 16:01:00 【明朗晨光】
流程
二叉树的层序遍历,就是图的宽度优先遍历,用队列实现:
①准备一个队列,根节点入队
②当前队首元素出队cur,打印
③cur有左孩子就左孩子入队,有右孩子就右孩子入队,即先左后右;
④重复② ~ ③,直到队列空为止。
代码实现
C++ 版
/************************************************************************* > File Name: 028.二叉树的层序遍历.cpp > Author: Maureen > Mail: [email protected] > Created Time: 一 6/20 09:02:18 2022 ************************************************************************/
#include <iostream>
#include <queue>
using namespace std;
class TreeNode {
public:
int val;
TreeNode *lchild;
TreeNode *rchild;
TreeNode(int v) : val(v) {
}
};
void level(TreeNode *root) {
if (root == nullptr) return ;
cout << "level-order:";
queue<TreeNode *> que;
que.push(root);
TreeNode *cur = nullptr;
while (!que.empty()) {
cur = que.front(); que.pop();
cout << cur->val << " ";
if (cur->lchild != nullptr) que.push(cur->lchild);
if (cur->rchild != nullptr) que.push(cur->rchild);
}
cout << endl;
}
int main() {
TreeNode *root = new TreeNode(1);
root->lchild = new TreeNode(2);
root->rchild = new TreeNode(3);
root->lchild->lchild = new TreeNode(4);
root->lchild->rchild = new TreeNode(5);
root->rchild->lchild = new TreeNode(6);
root->rchild->rchild = new TreeNode(7);
level(root);
return 0;
}
Java 版
import java.util.LinkedList;
import java.util.Queue;
public class LevelTraversalBT {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int v) {
value = v;
}
}
public static void level(Node head) {
if (head == null) {
return;
}
Queue<Node> queue = new LinkedList<>();
queue.add(head);
while (!queue.isEmpty()) {
Node cur = queue.poll();
System.out.println(cur.value);
if (cur.left != null) {
queue.add(cur.left);
}
if (cur.right != null) {
queue.add(cur.right);
}
}
}
public static void main(String[] args) {
Node head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.left.right = new Node(5);
head.right.left = new Node(6);
head.right.right = new Node(7);
level(head);
System.out.println("========");
}
}
边栏推荐
- Come and watch – tpt18 new report
- Web page automation practice 4. get the name, price and rating information of all hotels and write them into the file
- Deep understanding of zero copy technology
- 网购网站(期末大作业)
- 谷歌 Chrome 浏览器全新下载窗口将支持文件拖拽,Edge 已经支持
- 一个好产品应该具备的特征
- 2021数据库市场,Aerospike与顶级厂商争锋
- Esp8266 / esp32 obtenir la méthode NTP time via la Bibliothèque timelib
- 在线JSON转YAML工具
- Generating test reports using the unittest framework
猜你喜欢

快来围观–TPT18新版报到

海外new things | 美国人工智能初创「Zoovu」新一轮融资1.69亿美元,为消费者优化线上的“产品发现”体验

Esp8266/esp32 get NTP time method through timelib Library

Pytest framework

7 tips for writing effective help documents

机器学习中的概念漂移(Aporia)

The release of autok3s v0.5.0 continues to be simple and friendly

The "learning link" database of the learning software is suspected to have leaked information, revealing more than 100million pieces of student information

Cisco(35)——BGP入门实验

In 2021 database market, aerospike competes with top manufacturers
随机推荐
之前的装机记录
7 tips for writing effective help documents
Unittest框架
【SQLite】解决unrecognized token:“‘“
Huawei cloud releases desktop ide codearts
海外new things | 软件开发商「Dynaboard」种子轮融资660万美元,开发低代码平台连接设计、产品和开发人员
Esp8266/esp32 get NTP time method through timelib Library
[mathematical modeling] Matlab Application Practice Series (95) - application cases of time series prediction (with matlab code)
学习软件“学习通”数据库疑似发生信息泄露,泄露学生信息达1亿多条
string类的模拟实现
Alibaba cloud server + pagoda panel + no domain name deployment web project
Yaml数据驱动演示
强化学习入门项目spinning up(1)安装
使用 Guzzle 中间件进行优雅的请求重试
Huawei (13) - route introduction
Notice on printing and distributing the Interim Measures of Beijing Municipality for the administration of housing with common property rights
Unable to boot device in current state: started - unable to boot device in current state: booted
鲁班会开发者深度论坛丨与成都伙伴一起 “洞见物联网新风潮”
Advanced performance test series 4. premise, tool and process of performance test
[learn FPGA programming from scratch -38]: Advanced - syntax - functions and tasks