当前位置:网站首页>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(); */
边栏推荐
- Opencv interview guide
- Vgg16 migration learning source code
- Application of 51 single chip microcomputer timer
- It is difficult to quantify the extent to which a single-chip computer can find a job
- el-table X轴方向(横向)滚动条默认滑到右边
- 4G module at command communication package interface designed by charging pile
- QT is a method of batch modifying the style of a certain type of control after naming the control
- LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
- My 4G smart charging pile gateway design and development related articles
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
猜你喜欢

Installation and removal of MySQL under Windows

Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving

2. Elment UI date selector formatting problem
![[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer](/img/90/4de927e797ec9c2bb70e507392bed0.jpg)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer

yocto 技术分享第四期:自定义增加软件包支持

没有多少人能够最终把自己的兴趣带到大学毕业上

Development of intelligent charging pile (I): overview of the overall design of the system

Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)

Leetcode 300 最长上升子序列

Retinaface: single stage dense face localization in the wild
随机推荐
Opencv notes 17 template matching
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Wireshark use
4G module initialization of charge point design
yocto 技术分享第四期:自定义增加软件包支持
Swing transformer details-1
Octave instructions
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
QT is a method of batch modifying the style of a certain type of control after naming the control
Drive and control program of Dianchuan charging board for charging pile design
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
Working mode of 80C51 Serial Port
2021-10-27
ADS simulation design of class AB RF power amplifier
Emballage automatique et déballage compris? Quel est le principe?
JS基础-原型原型链和宏任务/微任务/事件机制
Timer and counter of 51 single chip microcomputer
My notes on intelligent charging pile development (II): overview of system hardware circuit design
2020-08-23