当前位置:网站首页>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(); */
边栏推荐
- 內存數據庫究竟是如何發揮內存優勢的?
- Design of charging pile mqtt transplantation based on 4G EC20 module
- 自動裝箱與拆箱了解嗎?原理是什麼?
- SCM is now overwhelming, a wide variety, so that developers are overwhelmed
- Code word in NR
- Sending and interrupt receiving of STM32 serial port
- is_ power_ of_ 2 judge whether it is a multiple of 2
- Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
- Exception handling of arm
- My 4G smart charging pile gateway design and development related articles
猜你喜欢

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

学习开发没有捷径,也几乎不存在带路会学的快一些的情况

My notes on the development of intelligent charging pile (III): overview of the overall design of the system software

Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~

单片机学到什么程度能找到工作,这个标准不好量化

手机都算是单片机的一种,只不过它用的硬件不是51的芯片

Project cost management__ Cost management technology__ Article 6 prediction

For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
C language enumeration type

干单片机这一行的时候根本没想过这么多,只想着先挣钱养活自己
随机推荐
My 4G smart charging pile gateway design and development related articles
The third paper of information system project manager in soft examination
Project cost management__ Cost management technology__ Article 8 performance review
(1) What is a lambda expression
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
Pymssql controls SQL for Chinese queries
STM32 external interrupt experiment
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
MYSQL数据库底层基础专栏
Vector processor 9_ Basic multilevel interconnection network
In third tier cities and counties, it is difficult to get 10K after graduation
Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
Screen display of charging pile design -- led driver ta6932
MySQL的简单使用(增删改查)
万字手撕七大排序(代码+动图演示)
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
我想各位朋友都应该知道学习的基本规律就是:从易到难
Fundamentals of Electronic Technology (III)_ Integrated operational amplifier and its application__ Basic arithmetic circuit
01仿B站项目业务架构
01 business structure of imitation station B project