当前位置:网站首页>[binary tree] completeness test of binary tree
[binary tree] completeness test of binary tree
2022-06-23 04:37:00 【It's so cold】
0x00 subject
Given a binary tree root
Determine if it's a Perfect binary tree
In a Completely Binary tree in
except Last Outside a node
All nodes are Completely Filled
And all nodes in the last node
As far as possible Keep to the left Of
It can contain 1 To 2h The last level between nodes h
0x01 Ideas
According to the definition of complete binary tree
Under what circumstances is No Complete binary tree ?
It's the emergence of Empty child nodes Then it appeared again Non empty child node
0x02 solution
Language :Swift
Tree node :TreeNode
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
self.left = left
self.right = right
}
}
solution :
func isCompleteTree(_ root: TreeNode?) -> Bool {
var queue: [TreeNode?] = []
// Whether the record traverses to Blank nodes
var flag: Bool = false
queue.append(root)
while !queue.isEmpty {
let node = queue.removeFirst()
if node == nil {
// An empty node appears
flag = true
continue
}else{
// After an empty node appears , Non empty nodes appear again , So it's not a complete binary tree
if flag {
return false
}
queue.append(node?.left)
queue.append(node?.right)
}
}
return true
}
0x03 My work
Welcome to experience one of my works : Little notes -XNote
Take notes one step at a time App Store Search ~
边栏推荐
- 众昂矿业:新能源新材料产业链对萤石需求大增
- After Huawei online battle service players quickly match, different players receive different lists of players in the same room
- 靜態查找錶和靜態查找錶
- x24Cxx系列EEPROM芯片C语言通用读写程序
- Flutter怎么实现不同缩放动画效果
- 虫子 STM32 高级定时器 (哈哈我说实话硬件定时器不能体现实力,实际上想把内核定时器发上来的,一想算了,慢慢来吧)
- Twitter cooperates with Shopify to introduce merchant products into twitter shopping
- Halcon glue line detection - template matching, pose transformation, glue width, glue continuity detection
- PTA:7-60 宠物的生长
- How node+express operates cookies
猜你喜欢

mysql如何删除表的一行数据

Deploying Apache pulsar on kubesphere

Implementation of VGA protocol based on FPGA

JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology

语料库数据处理个案实例(词性赋码、词性还原)

How does flutter achieve different zoom animation effects

Leetcode 1208. 尽可能使字符串相等(终于解决,晚安)

Redis启动有问题

背景彩带动画插件ribbon.js

在 KubeSphere 上部署 Apache Pulsar
随机推荐
城链科技董事长肖金伟:践行数据经济系国家战略,引领数字时代新消费发展!
Inscription of lougu brush
Ms-fsrvp forced abuse of POC
Leetcode 1208. 尽可能使字符串相等(终于解决,晚安)
[advanced binary tree] AVLTree - balanced binary search tree
JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
摆烂LuoGu刷题记
P1347 排序(topo)
After Huawei online battle service players quickly match, different players receive different lists of players in the same room
Monitoring artifact ZABBIX, from deployment to application, goes deep layer by layer
[从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险
【一起上水硕系列】Day Three - preview4
Bug STM32 advanced timer (haha, to tell you the truth, the hardware timer can't reflect its strength. In fact, I want to send the kernel timer. Just think about it. Take your time)
Leetcode 1208. 尽可能使字符串相等
Avltree - arbre de recherche binaire équilibré
QMainWindow
一篇文章学会er图绘制
支持在 Kubernetes 运行,添加多种连接器,SeaTunnel 2.1.2 版本正式发布!
Similar to RZ / SZ, trzsz supporting TMUX has released a new version
12 excellent practices of wireless network security