当前位置:网站首页>LeetCode - 919. 完全二叉树插入器 (数组)
LeetCode - 919. 完全二叉树插入器 (数组)
2022-07-03 09:20:00 【三岁就很萌@D】


数组

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class CBTInserter {
private TreeNode[] treeArray;
private int index;
public CBTInserter(TreeNode root) {
treeArray = new TreeNode[2000];
index = 0;
if(root!=null){
Deque<TreeNode> deque = new LinkedList<>();
deque.offerLast(root);
while(deque.size() > 0){
TreeNode peek = deque.pollFirst();
if(peek.left!=null)
deque.offerLast(peek.left);
if(peek.right!=null)
deque.offerLast(peek.right);
treeArray[index++] = peek;
}
}
}
public int insert(int val) {
int parent = (index - 1) / 2;
int child = index;
TreeNode node = new TreeNode(val);
treeArray[index++] = node;
if(parent>=0)
{
TreeNode pn = treeArray[parent];
if(child % 2 == 1)
pn.left = node;
else
pn.right = node;
}
return treeArray[parent].val;
}
public TreeNode get_root() {
return treeArray[0];
}
}
/** * Your CBTInserter object will be instantiated and called as such: * CBTInserter obj = new CBTInserter(root); * int param_1 = obj.insert(val); * TreeNode param_2 = obj.get_root(); */
边栏推荐
- el-table X轴方向(横向)滚动条默认滑到右边
- Application of 51 single chip microcomputer timer
- El table X-axis direction (horizontal) scroll bar slides to the right by default
- uniapp 实现微信小程序全局分享及自定义分享按钮样式
- Project cost management__ Cost management technology__ Article 8 performance review
- STM32 interrupt priority management
- 01仿B站项目业务架构
- 2020-08-23
- I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
- [CSDN] C1 training problem analysis_ Part IV_ Advanced web
猜你喜欢

NR PUCCH format0 sequence generation and detection mechanism

Stm32-hal library learning, using cubemx to generate program framework

Hal library sets STM32 clock

GPIO port details, Hal library operation keys

Mysql database underlying foundation column

Vector processor 9_ Basic multilevel interconnection network

内存数据库究竟是如何发挥内存优势的?

Oracle database SQL statement execution plan, statement tracking and optimization instance

Adaptiveavgpool1d internal implementation

Uniapp realizes global sharing of wechat applet and custom sharing button style
随机推荐
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
Development of intelligent charging pile (I): overview of the overall design of the system
JS基础-原型原型链和宏任务/微任务/事件机制
一个可执行的二进制文件包含的不仅仅是机器指令
(2) New methods in the interface
C language enumeration type
Gpiof6, 7, 8 configuration
【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进
byte alignment
Exception handling of arm
Code word in NR
内存数据库究竟是如何发挥内存优势的?
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
4G module initialization of charge point design
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
Basic knowledge of communication interface
嵌入式系统没有特别明确的定义
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode