当前位置:网站首页>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(); */
边栏推荐
- Quelle langue choisir pour programmer un micro - ordinateur à puce unique
- STM32 interrupt priority management
- My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
- 01仿B站项目业务架构
- 自動裝箱與拆箱了解嗎?原理是什麼?
- 没有多少人能够最终把自己的兴趣带到大学毕业上
- 学习开发没有捷径,也几乎不存在带路会学的快一些的情况
- 03 fastjason solves circular references
- Oracle database SQL statement execution plan, statement tracking and optimization instance
- 4G module at command communication package interface designed by charging pile
猜你喜欢

STM32 interrupt switch

51 MCU tmod and timer configuration

NR PUCCH format0 sequence generation and detection mechanism
![[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation](/img/06/7fd985faf8806ceface3864d4b3180.png)
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation

【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进

Not many people can finally bring their interests to college graduation

2.Elment Ui 日期选择器 格式化问题

I think all friends should know that the basic law of learning is: from easy to difficult

编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解

There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
随机推荐
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
byte alignment
单片机学到什么程度能找到工作,这个标准不好量化
Liquid crystal display
Application of external interrupts
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
yocto 技术分享第四期:自定义增加软件包支持
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
Project cost management__ Cost management technology__ Article 8 performance review
【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进
The third paper of information system project manager in soft examination
Not many people can finally bring their interests to college graduation
03 FastJson 解决循环引用
All processes of top ten management in project management
Swing transformer details-1
2020-08-23
Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
Code word in NR
SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers