当前位置:网站首页>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(); */
边栏推荐
- Circular queue related design and implementation reference 1
- Installation and removal of MySQL under Windows
- My 4G smart charging pile gateway design and development related articles
- [CSDN] C1 training problem analysis_ Part IV_ Advanced web
- 没有多少人能够最终把自己的兴趣带到大学毕业上
- 万字手撕七大排序(代码+动图演示)
- 01 business structure of imitation station B project
- 03 FastJson 解决循环引用
- Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
- My notes on intelligent charging pile development (II): overview of system hardware circuit design
猜你喜欢

STM32 interrupt switch
![[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer](/img/90/4de927e797ec9c2bb70e507392bed0.jpg)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer

Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit

STM32 interrupt priority management

Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction

Swing transformer details-2

万字手撕七大排序(代码+动图演示)

Stm32f407 key interrupt

Characteristics of PUCCH formats

STM32 serial port usart1 routine
随机推荐
Not many people can finally bring their interests to college graduation
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
How does the memory database give full play to the advantages of memory?
开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~
Crash工具基本使用及实战分享
(1) What is a lambda expression
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
Introduction to chromium embedded framework (CEF)
Hal library sets STM32 clock
Basic knowledge of MySQL database (an introduction to systematization)
端午节快乐!—— canvas写的粽子~~~~~
Do you understand automatic packing and unpacking? What is the principle?
2021-10-28
Fundamentals of Electronic Technology (III)__ Fundamentals of circuit analysis__ Basic amplifier operating principle
Design of charging pile mqtt transplantation based on 4G EC20 module
要選擇那種語言為單片機編寫程序呢
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
All processes of top ten management in project management
QT qcombobox QSS style settings
要选择那种语言为单片机编写程序呢