当前位置:网站首页>LeetCode - 919. Full binary tree inserter (array)
LeetCode - 919. Full binary tree inserter (array)
2022-07-03 10:06:00 【Cute at the age of three @d】


Array

/** * 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(); */
边栏推荐
- My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
- LeetCode - 933 最近的请求次数
- Design of charging pile mqtt transplantation based on 4G EC20 module
- [Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
- 01仿B站项目业务架构
- Opencv histogram equalization
- LeetCode - 919. 完全二叉树插入器 (数组)
- LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
- Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
- Do you understand automatic packing and unpacking? What is the principle?
猜你喜欢

Which language should I choose to program for single chip microcomputer

El table X-axis direction (horizontal) scroll bar slides to the right by default

The underlying principle of vector

Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
![[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)](/img/19/5dc152b3fadeb56de50768561ad659.jpg)
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)

2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)

I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first

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

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍

Yocto Technology Sharing Phase 4: Custom add package support
随机推荐
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
使用密钥对的形式连接阿里云服务器
Uniapp realizes global sharing of wechat applet and custom sharing button style
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
pycharm 无法引入自定义包
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
Which language should I choose to program for single chip microcomputer
El table X-axis direction (horizontal) scroll bar slides to the right by default
2021-11-11 standard thread library
使用sed替换文件夹下文件
Emballage automatique et déballage compris? Quel est le principe?
Pymssql controls SQL for Chinese queries
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
Problems encountered when MySQL saves CSV files
(1) 什么是Lambda表达式
Basic knowledge of communication interface
Positive and negative sample division and architecture understanding in image classification and target detection
Interruption system of 51 single chip microcomputer
STM32 interrupt switch
2021-01-03