当前位置:网站首页>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(); */
边栏推荐
- 01仿B站项目业务架构
- MySQL的简单使用(增删改查)
- 要選擇那種語言為單片機編寫程序呢
- Stm32 NVIC interrupt priority management
- The third paper of information system project manager in soft examination
- 应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
- Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
- 嵌入式系统没有特别明确的定义
- Notes on C language learning of migrant workers majoring in electronic information engineering
- 内存数据库究竟是如何发挥内存优势的?
猜你喜欢
yocto 技术分享第四期:自定义增加软件包支持
Code word in NR
Fundamentals of Electronic Technology (III)__ Fundamentals of circuit analysis__ Basic amplifier operating principle
Exception handling of arm
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
内存数据库究竟是如何发挥内存优势的?
UCI and data multiplexing are transmitted on Pusch (Part VI) -- LDPC coding
03 fastjason solves circular references
我想各位朋友都应该知道学习的基本规律就是:从易到难
Fundamentals of Electronic Technology (III)_ Integrated operational amplifier and its application__ Basic arithmetic circuit
随机推荐
STM32 running lantern experiment - library function version
It is difficult to quantify the extent to which a single-chip computer can find a job
A lottery like scissors, stone and cloth (C language)
Adaptiveavgpool1d internal implementation
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
Vector processor 9_ Basic multilevel interconnection network
4G module IMEI of charging pile design
Notes on C language learning of migrant workers majoring in electronic information engineering
4G module initialization of charge point design
Idea remote breakpoint debugging jar package project
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
我想各位朋友都应该知道学习的基本规律就是:从易到难
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
(1) What is a lambda expression
Project cost management__ Cost management technology__ Article 7 completion performance index (tcpi)
Introduction to chromium embedded framework (CEF)
03 FastJson 解决循环引用
Synchronization control between tasks
Simple use of MySQL (addition, deletion, modification and query)
Interruption system of 51 single chip microcomputer