当前位置:网站首页>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(); */
边栏推荐
- (1) What is a lambda expression
- Application of external interrupts
- SCM is now overwhelming, a wide variety, so that developers are overwhelmed
- STM32 interrupt switch
- Dictionary tree prefix tree trie
- openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
- Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
- MySQL root user needs sudo login
- My notes on intelligent charging pile development (II): overview of system hardware circuit design
- There is no specific definition of embedded system
猜你喜欢

Notes on C language learning of migrant workers majoring in electronic information engineering

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

Installation and removal of MySQL under Windows

Stm32f407 key interrupt
![[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)

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

Opencv feature extraction sift

In third tier cities and counties, it is difficult to get 10K after graduation

单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇

Opencv Harris corner detection
随机推荐
2. Elment UI date selector formatting problem
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
Basic knowledge of MySQL database (an introduction to systematization)
Qcombox style settings
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
STM32 general timer 1s delay to realize LED flashing
Simple use of MySQL (addition, deletion, modification and query)
LeetCode - 706 设计哈希映射(设计) *
Window maximum and minimum settings
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
2020-08-23
Wireshark use
Adaptiveavgpool1d internal implementation
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
01 business structure of imitation station B project
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
is_ power_ of_ 2 judge whether it is a multiple of 2
An executable binary file contains more than machine instructions
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program