当前位置:网站首页>Leetcode · daily question · 919. complete binary tree inserter · hierarchy traversal · BFS
Leetcode · daily question · 919. complete binary tree inserter · hierarchy traversal · BFS
2022-07-26 03:12:00 【Xiao Xun wants to be strong】
link :https://leetcode.cn/problems/complete-binary-tree-inserter/solution/by-xun-ge-v-anga/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
subject

Example

Ideas
Their thinking
Breadth first search
The title requires a binary tree , Every time a new node is inserted, the tree must be a complete binary tree
- A complete binary tree is every layer ( 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 .
It's not hard to find out , Actually, the hierarchy of the tree is traversed -> Check each floor Just enough to traverse the tree , Check the insertion point of the complete binary tree , Because every time, all nodes of the first layer are checked, and the priority left side is also met , The tree is traversed hierarchically every time it is inserted , Find the first notch, which is the insertion point
Concrete realization
For hierarchical traversal implementation , There are two ways , The first kind of recursion , The second queue iteration , The title requires us to return to the parent node of the insertion point , But recursion will recursively enter , The parent node is not well preserved , So it is more convenient to use queue iteration
Queue iteration realizes post order traversal
Define a queue , The size is 10000, Because the topic requires to call functions at most 10000 Time , First put the header node into the queue , Then pop up to determine whether there are left and right nodes , Exist and join the team , If it does not exist, you can insert a new node , And return the current node value .
Code
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
typedef struct {// initialization
struct TreeNode * root;
} CBTInserter;
// initialization
CBTInserter* cBTInserterCreate(struct TreeNode* root) {
CBTInserter * obj = malloc(sizeof(CBTInserter));
obj->root = root;
return obj;
}
// Iterative implementation of hierarchical traversal
int cBTInserterInsert(CBTInserter* obj, int val) {
struct TreeNode * next = obj->root;
struct TreeNode * res[10000];
/*struct TreeNode * node = malloc(sizeof(struct TreeNode));// Initialize new insertion point
node->val = val;
node->left = NULL;
node->right = NULL;*/
int l = 0;
int r = 0;
res[r++] = next;// Head node join the team
while(l < r)// Level traversal
{
int rr = r;
while(l < rr)// Go through each layer
{
if(res[l]->left)// Determine whether the left and right nodes exist
{
res[r++] = res[l]->left;
}
else
{
struct TreeNode * node = malloc(sizeof(struct TreeNode));// Initialize new insertion point
node->val = val;
node->left = NULL;
node->right = NULL;
res[l]->left = node;
return res[l]->val;
}
if(res[l]->right)
{
res[r++] = res[l]->right;
}
else
{
struct TreeNode * node = malloc(sizeof(struct TreeNode));// Initialize new insertion point
node->val = val;
node->left = NULL;
node->right = NULL;
res[l]->right = node;
return res[l]->val;
}
l++;
}
}
return -1;
}
struct TreeNode* cBTInserterGet_root(CBTInserter* obj) {
return obj->root;
}
void TreeFree(struct TreeNode * root)
{
if(root == NULL)
{
return;
}
TreeFree(root->left);
TreeFree(root->right);
free(root);
}
void cBTInserterFree(CBTInserter* obj) {// Recursively destroy the tree
TreeFree(obj->root);
free(obj);
}
/**
* Your CBTInserter struct will be instantiated and called as such:
* CBTInserter* obj = cBTInserterCreate(root);
* int param_1 = cBTInserterInsert(obj, val);
* struct TreeNode* param_2 = cBTInserterGet_root(obj);
* cBTInserterFree(obj);
*/
author :xun-ge-v
link :https://leetcode.cn/problems/complete-binary-tree-inserter/solution/by-xun-ge-v-anga/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .Time and space complexity

边栏推荐
- STM32 - PWM learning notes
- [noip2001 popularization group] the problem of maximum common divisor and minimum common multiple
- canvas——心电图的设计,以及如何清理画布
- Functions and usage of snownlp Library
- GoLang日志编程系统
- LeetCode·
- Opencv报错:(parameter or structure field))Unrecognized or unsupported array type in functon ‘cvGetMat‘
- STM——EXTI外部中断学习笔记
- 【TensorFlow&PyTorch】图像数据增强API
- LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
猜你喜欢
![[untitled]](/img/6f/a2cd98af7a8de469e5311422b48afe.png)
[untitled]

Influence of middle tap change on ZVS oscillation circuit

【尤里复裂人】带你轻松理解——深拷贝和浅拷贝

c语言分层理解(c语言函数)

Leetcode · daily question · sword finger offer | | 115. reconstruction sequence · topological sorting

Three years of software testing experience, salary has been stuck at 10K, how to improve and develop automated testing?
![[translation] cloud like internal load balancer for kubernetes?](/img/e5/f003ebed05a94d2936cfef5617f745.jpg)
[translation] cloud like internal load balancer for kubernetes?
![[detailed explanation of key and difficult points of document operation]](/img/f5/99c8cdf09763c66ab5d56cc96e50c7.png)
[detailed explanation of key and difficult points of document operation]

Win11隐藏输入法状态栏方法

Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic
随机推荐
Win11隐藏输入法状态栏方法
文件操作(一)——文件简介与文件的打开方式和关闭
LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
How to close the case prompt icon of win11? Closing method of win11 case prompt Icon
78. 子集
STM32 - serial port learning notes (one byte, 16 bit data, string, array)
STM32 - PWM learning notes
Win11 hide input method status bar method
dataframe整理:datetime格式分拆;删除特定行;分组整合。
如何用U盘进行装机?
Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered
班级里有一群学生考试结果出来了,考了语文和数学两门,请筛选出总分是第一的同学
What are the methods of array sorting in JS
图解LeetCode——5. 最长回文子串(难度:中等)
How to design test cases according to the requirements of login testing?
C language layered understanding (C language function)
ByteDance (Tiktok) software test monthly salary 23K post, technical two-sided interview questions are newly released
Be highly vigilant! Weaponization of smartphone location data on the battlefield
Digital commerce cloud DMS dealer management system solution: DMS system realizes business Omni channel and sales data collection
[translation] announce Vites 13