当前位置:网站首页>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(); */
边栏推荐
- Windows下MySQL的安装和删除
- Stm32 NVIC interrupt priority management
- GPIO port details, Hal library operation keys
- 学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
- 4G module IMEI of charging pile design
- 2020-08-23
- STM32 serial port usart1 routine
- Design of charging pile mqtt transplantation based on 4G EC20 module
- Oracle database SQL statement execution plan, statement tracking and optimization instance
- Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
猜你喜欢
Fundamentals of Electronic Technology (III)__ Fundamentals of circuit analysis__ Basic amplifier operating principle
CEF download, compile project
UCI and data multiplexing are transmitted on Pusch (Part VI) -- LDPC coding
內存數據庫究竟是如何發揮內存優勢的?
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
Development of intelligent charging pile (I): overview of the overall design of the system
Fundamentals of Electronic Technology (III)_ Integrated operational amplifier and its application__ Basic arithmetic circuit
Timer and counter of 51 single chip microcomputer
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
STM32 interrupt switch
随机推荐
2020-08-23
4G module initialization of charge point design
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
03 fastjason solves circular references
Do you understand automatic packing and unpacking? What is the principle?
Swing transformer details-1
Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
getopt_ Typical use of long function
03 fastjason solves circular references
An executable binary file contains more than machine instructions
(2) New methods in the interface
STM32 external interrupt experiment
Uniapp realizes global sharing of wechat applet and custom sharing button style
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
Design of charging pile mqtt transplantation based on 4G EC20 module
Installation and removal of MySQL under Windows
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
STM32 port multiplexing and remapping
2020-08-23
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走