当前位置:网站首页>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(); */
边栏推荐
- 学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
- (2) New methods in the interface
- The third paper of information system project manager in soft examination
- STM32 serial communication principle
- Fundamentals of Electronic Technology (III)_ Integrated operational amplifier and its application__ Basic arithmetic circuit
- Getting started with JMX, MBean, mxbean, mbeanserver
- 2020-08-23
- Simple use of MySQL (addition, deletion, modification and query)
- Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
- 2.Elment Ui 日期选择器 格式化问题
猜你喜欢

在三线城市、在县城,很难毕业就拿到10K

Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from

An executable binary file contains more than machine instructions

STM32 interrupt priority management

Blue Bridge Cup for migrant workers majoring in electronic information engineering

STM32 interrupt switch

单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了

单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇

Code word in NR

There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
随机推荐
My 4G smart charging pile gateway design and development related articles
(1) 什么是Lambda表达式
Installation and removal of MySQL under Windows
Vector processor 9_ Basic multilevel interconnection network
在三线城市、在县城,很难毕业就拿到10K
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~
嵌入式系统没有特别明确的定义
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
4G module initialization of charge point design
Project cost management__ Cost management technology__ Article 8 performance review
(2)接口中新增的方法
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
我想各位朋友都应该知道学习的基本规律就是:从易到难
In third tier cities and counties, it is difficult to get 10K after graduation
一个可执行的二进制文件包含的不仅仅是机器指令
03 FastJson 解决循环引用
01 business structure of imitation station B project