当前位置:网站首页>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(); */
边栏推荐
- LeetCode - 705 设计哈希集合(设计)
- Do you understand automatic packing and unpacking? What is the principle?
- 01仿B站项目业务架构
- Tensorflow built-in evaluation
- An executable binary file contains more than machine instructions
- Qcombox style settings
- (2)接口中新增的方法
- Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
- 01 business structure of imitation station B project
- Positive and negative sample division and architecture understanding in image classification and target detection
猜你喜欢

JS基础-原型原型链和宏任务/微任务/事件机制

LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)

MySQL root user needs sudo login

The underlying principle of vector

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

Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip

It is difficult to quantify the extent to which a single-chip computer can find a job

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

2021-10-28

Opencv gray histogram, histogram specification
随机推荐
Not many people can finally bring their interests to college graduation
Basic knowledge of MySQL database (an introduction to systematization)
Stm32 NVIC interrupt priority management
01仿B站项目业务架构
Blue Bridge Cup for migrant workers majoring in electronic information engineering
2021-11-11 standard thread library
LeetCode - 933 最近的请求次数
Wireshark use
Opencv notes 17 template matching
ADS simulation design of class AB RF power amplifier
Windows下MySQL的安装和删除
Adaptiveavgpool1d internal implementation
Yocto technology sharing phase IV: customize and add software package support
Exception handling of arm
Toolbutton property settings
Vgg16 migration learning source code
Dictionary tree prefix tree trie
4G module designed by charging pile obtains signal strength and quality
2. Elment UI date selector formatting problem
Simulate mouse click