当前位置:网站首页>[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 ~
边栏推荐
猜你喜欢

Pytoch --- pytoch customizes the dataset

背景彩带动画插件ribbon.js

Sessions and Daemons

【Pytorch】用自动微分求sin(x)的导数

photoshop PS 查看像素坐标、像素颜色、像素HSB颜色

A summary of PostgreSQL data types. All the people are here

基于FPGA的VGA协议实现

How to use MySQL index well

Flutter怎么实现不同缩放动画效果

支持在 Kubernetes 运行,添加多种连接器,SeaTunnel 2.1.2 版本正式发布!
随机推荐
什么是元数据
华为联机对战服务玩家快速匹配后,不同玩家收到的同一房间内玩家列表不同
Dynamics 365 插件中权限操作
If you want to understand PostgreSQL, you must first brush the architecture
QMainWindow
最新编程语言排行榜
P1347 排序(topo)
移动端城市列表排序js插件vercitylist.js
Photoshop PS viewing pixel coordinates, pixel colors, pixel HSB colors
PTA:7-31 期刊收费
【二叉樹進階】AVLTree - 平衡二叉搜索樹
Ms-fsrvp forced abuse of POC
mysql能不能在linux中使用
Pytoch --- use pytoch's pre training model to realize four weather classification problems
PTA:7-63 计算高考状元
PTA:6-71 时钟模拟
② cocoapods原理及 PodSpec 文件上传操作
[从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险
在线文本过滤小于指定长度工具
炫酷鼠标跟随动画js插件5种