当前位置:网站首页>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 - 703 数据流中的第 K 大元素(设计 - 优先队列)
- Liquid crystal display
- STM32 general timer output PWM control steering gear
- LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
- 4G module at command communication package interface designed by charging pile
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- Not many people can finally bring their interests to college graduation
- Drive and control program of Dianchuan charging board for charging pile design
- Application of 51 single chip microcomputer timer
- 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
猜你喜欢
2.Elment Ui 日期选择器 格式化问题
51 MCU tmod and timer configuration
Timer and counter of 51 single chip microcomputer
In third tier cities and counties, it is difficult to get 10K after graduation
没有多少人能够最终把自己的兴趣带到大学毕业上
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
Not many people can finally bring their interests to college graduation
yocto 技術分享第四期:自定義增加軟件包支持
Interruption system of 51 single chip microcomputer
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
Not many people can finally bring their interests to college graduation
On the problem of reference assignment to reference
2. Elment UI date selector formatting problem
QT detection card reader analog keyboard input
STM32 running lantern experiment - library function version
Vscode markdown export PDF error
2021-01-03
LeetCode - 706 设计哈希映射(设计) *
Notes on C language learning of migrant workers majoring in electronic information engineering
Emballage automatique et déballage compris? Quel est le principe?
QT is a method of batch modifying the style of a certain type of control after naming the control
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
Opencv note 21 frequency domain filtering
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
Synchronization control between tasks