当前位置:网站首页>Complete binary tree
Complete binary tree
2022-04-22 06:11:00 【Birds follow February】
Ideas : Sequence traversal binary tree
If a node , Left and right children are not empty , be pop The node , Put their left and right children in line
If a node , The left child is empty , The right child is not empty , Then the tree must not be a complete binary tree
If a node , The left child is not empty , The right child is empty ; Or left and right children are empty , Then all the nodes in the queue after the node are leaf nodes ; This tree is a complete binary tree , Otherwise return to false.
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static boolean isCBT(Node head) {
if (head == null) {
return true;
}
Queue<Node> queue = new LinkedList<Node>();
boolean leaf = false;
Node l = null;
Node r = null;
queue.offer(head);
while (!queue.isEmpty()) {
head = queue.poll();
l = head.left;
r = head.right;
if ((leaf && (l != null || r != null)) || (l == null && r != null)) {
return false;// The current node is not a leaf node and the previous node has a leaf node || The current node has right children but no left children
}
if (l != null) {
queue.offer(l);
}
if (r != null) {
queue.offer(r);
} else {
leaf = true;// No child is a leaf node
}
}
return true;
}
版权声明
本文为[Birds follow February]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220542258879.html
边栏推荐
- Heap basic operation source code Bing
- 《C语言程序设计现代方法》阅读笔记
- 转义符\ 数据格式的拼接
- Geojson file ShapeFile file batch conversion gadget
- jeecgboot开发经验过程
- QToolButtom、QPushButtom添加QMenu后去掉右下角三角图标
- ADC key for learning of Blue Bridge Cup embedded expansion board
- 指针所指的地址值及指针所指对象的值(学习笔记)
- Image pyramid, edge detection, image weighted display, histogram equalization
- Invalid transform origin base point setting
猜你喜欢

Ad embedded learning blue bridge

关于常量指针、指针常量的使用--练习题(记录)

QT drawPixmap和drawImage模糊问题

快应用模糊搜索

Interaction method II between STM32 single chip microcomputer and ld3320 voice module

Part 90 leetcode refers to the longest substring of offer dynamic programming (VII) that does not contain duplicate characters

Pykmip test

jeecgboot-online表单开发-控件配置

Dynamic memory management, file operation, preprocessing

Installation of QT learning
随机推荐
常见面试题 - 4(MySQL)
AD5724 bipolar ADC
图像金字塔、边缘检测、图像加权显示、直方图均衡化
Out range of signed 32bit display when compiling openssl-0.9.8e
Error in QT: undefined reference to ` widget:: settime()‘
Markdown 语法支持测试
Introduction to Intel SGX development
两指针相加?(合法or不合法)
Photoresist for learning of Blue Bridge Cup embedded expansion board
Chapter 89 leetcode refers to offer dynamic programming (6) translating numbers into strings
QT drawPixmap和drawImage模糊问题
2020-10-28
Compiling OpenSSL on HP UNIX and using
Dlopen calls dynamic library
Jeecgboot online development 3
单、多通道图像反差处理
QWebsocket通信
Write an article about DDT data-driven automated testing
关于常量指针、指针常量的使用--练习题(记录)
Intel SGX初步学习理解笔记(持续更新)