当前位置:网站首页>golang刷leetcode 经典(12) 完全二叉树插入器
golang刷leetcode 经典(12) 完全二叉树插入器
2022-08-02 18:54:00 【用户9710217】
完全二叉树是每一层(除最后一层外)都是完全填充(即,结点数达到最大)的,并且所有的结点都尽可能地集中在左侧。
设计一个用完全二叉树初始化的数据结构 CBTInserter,它支持以下几种操作:
CBTInserter(TreeNode root) 使用头结点为 root 的给定树初始化该数据结构;
CBTInserter.insert(int v) 将 TreeNode 插入到存在值为 node.val = v 的树中以使其保持完全二叉树的状态,并返回插入的 TreeNode 的父结点的值;
CBTInserter.get_root() 将返回树的头结点。
示例 1:
输入:inputs = ["CBTInserter","insert","get_root"], inputs = [[[1]],[2],[]]
输出:[null,1,[1,2]]
示例 2:
输入:inputs = ["CBTInserter","insert","insert","get_root"], inputs = [[[1,2,3,4,5,6]],[7],[8],[]]
输出:[null,3,4,[1,2,3,4,5,6,7,8]]提示:
最初给定的树是完全二叉树,且包含 1 到 1000 个结点。
每个测试用例最多调用 CBTInserter.insert 操作 10000 次。
给定结点或插入结点的每个值都在 0 到 5000 之间。
解题思路
1,对于完全二叉树类型的题目,一般解法有俩:
A,用数字记录下标index
B,双端队列层序遍历
2,对于本题采用B方案
3,数据结构设计
A,用root存储树的根节点
B,用current存储当前要插入孩子的节点,即新数据的插入位置
C,dqueue 依次存储孩子不满的节点
4,需要注意的坑
A,如果左孩子为空,则下次插入左孩子
B,如果右孩子为空,构建的时候不要忘了把左孩子入队列
C,如果右孩子为空,插入元素后,下一个current位置是队首
代码实现
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type CBTInserter struct {
root *TreeNode
current *TreeNode
dqueue []*TreeNode
}
func (this* CBTInserter)pop()*TreeNode{
d:=this.dqueue[0]
this.dqueue=this.dqueue[1:]
return d
}
func(this*CBTInserter)push(n*TreeNode){
this.dqueue=append(this.dqueue,n)
}
func(this*CBTInserter)empty()bool{
return len(this.dqueue)==0
}
func(this*CBTInserter)Print(){
fmt.Println("")
for _,v:=range this.dqueue{
fmt.Print(v.Val)
}
}
func Constructor(root *TreeNode) CBTInserter {
cbt:=CBTInserter{
root:root,
current:root,
}
if root==nil{
return cbt
}
cbt.push(root)
for !cbt.empty(){
node:=cbt.pop()
if node.Left==nil {
cbt.current=node
//cbt.Print()
return cbt
}
if node.Right==nil{
cbt.current=node
cbt.push(node.Left)
return cbt
}
cbt.push(node.Left)
cbt.push(node.Right)
}
//cbt.Print()
return cbt
}
func (this *CBTInserter) Insert(v int) int {
n:=&TreeNode{
Val:v,
}
if this.root==nil{
this.root=n
this.current=n
return v
}
var val int
if this.current.Left==nil{
this.current.Left=n
val=this.current.Val
}else{
//if this.current.Right==nil{
this.current.Right=n
val=this.current.Val
//this.Print()
if !this.empty(){
this.current=this.pop()
}
}
this.push(n)
return val
}
func (this *CBTInserter) Get_root() *TreeNode {
return this.root
}
/**
* Your CBTInserter object will be instantiated and called as such:
* obj := Constructor(root);
* param_1 := obj.Insert(v);
* param_2 := obj.Get_root();
*/边栏推荐
猜你喜欢

7.25 - 每日一题 - 408

备战无人机配送:互联网派To C、技术派To B

Boyun Selected as Gartner China DevOps Representative Vendor

浅谈一下pyd文件的逆向

项目分析(复杂嵌入式系统设计)

有哪些好用的实时网络流量监控软件

看【C语言】实现简易计算器教程,让小伙伴们为你竖起大拇指

thinkphp框架5.0.23安全更新问题-漏洞修复-/thinkphp/library/think/App.php具体怎么改以及为什么要这么改

Based on OpenGL glaciers and firebird (illumination calculation model, visual, particle system)

研发了 5 年的时序数据库,到底要解决什么问题?
随机推荐
WIFi 开关控制实现-ESP8266 物联网 android studio arduino QT多线程服务器
动态规划常见实例详解
简单有效又有用的关闭antimalware service executable的方法·备份记录
How to mitigate the attack of corporate account hijacking?
T5: Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer
浅谈一下pyd文件的逆向
洛谷P2345 MooFest G
线程池原理与实践|从入门到放弃,深度解析
备战无人机配送:互联网派To C、技术派To B
回收站删除的文件怎么恢复,2个方法汇总助您快速解决
Why young people are snapping up domestic iPhone, because it is much cheaper and more populist
基于OpenGL的冰川与火鸟(光照计算模型、视景体、粒子系统)
荐号 | 当一个人不联系你,不拉黑你,原因只有一个……!
[深入研究4G/5G/6G专题-49]: 5G Link Adaption链路自适应-5-上行链路自适应ULLA-PUSCH信道
【C语言刷题】双指针原地修改数组(力扣原题分析)
【C语言刷题】牛客JZ65——不用四则运算作加法
Geoserver+mysql+openlayers
论文阅读_胶囊网络CapsNet
7.23 - 每日一题 - 408
Metaverse 001 | Can't control your emotions?The Metaverse is here to help you