当前位置:网站首页>Leetcode 0919. complete binary tree inserter: array representation of complete binary tree
Leetcode 0919. complete binary tree inserter: array representation of complete binary tree
2022-07-25 23:35:00 【Tisfy】
【LetMeFly】919. Complete binary tree inserter : Array representation of complete binary tree
Force button topic link :https://leetcode.cn/problems/complete-binary-tree-inserter/
Perfect binary tree Every floor ( Except for the last floor ) All completely filled ( namely , The number of nodes reaches the maximum ) Of , And all nodes are concentrated on the left as much as possible .
Design an algorithm , Insert a new node into a complete binary tree , And keep it intact after insertion .
Realization CBTInserter class :
CBTInserter(TreeNode root)Use the header node asrootInitialize the data structure for the given tree ;CBTInserter.insert(int v)Insert a value of... Into the treeNode.val == valThe new nodeTreeNode. Keep the tree in the state of a complete binary tree , And return to the insert nodeTreeNodeThe value of the parent node of ;CBTInserter.get_root()The head node of the tree will be returned .
Example 1:

Input ["CBTInserter", "insert", "insert", "get_root"] [[[1, 2]], [3], [4], []] Output [null, 1, 2, [1, 2, 3, 4]] explain CBTInserter cBTInserter = new CBTInserter([1, 2]); cBTInserter.insert(3); // return 1 cBTInserter.insert(4); // return 2 cBTInserter.get_root(); // return [1, 2, 3, 4]
Tips :
- The number of nodes in the tree ranges from
[1, 1000] 0 <= Node.val <= 5000rootIt's a perfect binary tree0 <= val <= 5000- Each test case calls at most
insertandget_rootoperation104Time
Method 1 : Use an array to store a complete binary tree
A complete binary tree has the following properties :

If the root node is from 1 Start numbering in a hierarchical way , that The number of the parent node = ⌊ The number of the child node 2 ⌋ The number of the parent node =\lfloor \frac{ The number of the child node }{2}\rfloor The number of the parent node =⌊2 The number of the child node ⌋
therefore , We can use arrays to store the nodes of a complete binary tree , So when adding new nodes , Directly append the new node to the end of the array , You can easily get the parent node of the new node O ( 1 ) O(1) O(1).
after , Point the child node of the parent node to the new node .
- Time complexity O ( n ) O(n) O(n), among n n n Is the number of nodes of the initial binary tree
- Spatial complexity O ( m ) O(m) O(m), among m m m Is the number of nodes of the final binary tree
AC Code
C++
class CBTInserter {
private:
vector<TreeNode*> a;
public:
CBTInserter(TreeNode* root) {
// Initial binary tree , Store the array in a hierarchical way
queue<TreeNode*> q;
q.push(root);
while (q.size()) {
TreeNode* p = q.front();
q.pop();
a.push_back(p);
if (p->left)
q.push(p->left);
if (p->right)
q.push(p->right);
}
}
int insert(int val) {
TreeNode* thisNode = new TreeNode(val); // New node
a.push_back(thisNode);
int th = a.size(); // Number of new node
TreeNode* father = a[th / 2 - 1]; // The number of the parent node = Number of new node / 2 ;-1 Because the subscript in the array is from 0 At first, the binary tree node starts from 1 Numbered starting
if (th % 2) {
// Odd numbers indicate left nodes
father->right = thisNode;
}
else {
father->left = thisNode;
}
return father->val;
}
TreeNode* get_root() {
return a[0]; // The root is the first node in the array
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125974862
边栏推荐
- [code case] blog page design (with complete source code)
- Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
- [QNX Hypervisor 2.2用户手册]9.7 generate
- Vscode shortcut key: collapse and expand code
- 谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
- Anti shake and throttling
- About priority queues
- Regular expression (user name form verification / verification of landline number / regular replacement)
- R语言安装教程 | 图文介绍超详细
- Kotlin 常用知识点汇总
猜你喜欢
随机推荐
Qpprogressbar for QT style (QSS) application
Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
WebMvcConfigurationSupport
新手哪个券商开户最好 开户最安全
Implementation of date class
chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
[JUC] concurrent keyword volatile
This point inside the function / change this point inside the function
[nodejs] nodejs create a simple server
Summary of kotlin common knowledge points
Solution of phpstudy service environment 80 port occupied by process system under Windows
Which securities firm is the best and safest for beginners to open an account
Npm+ module loading mechanism
图的遍历-DFS,BFS(代码详解)
程序员面试金典 4.12 求和路径
XxE & XML external entity injection utilization and bypass
【代码案例】博客页面设计(附完整源码)
E-commerce RPA, a magic weapon to promote easy entry
Mongodb query and projection operators
疫情之下的好消息









