当前位置:网站首页>Binary tree initialization code
Binary tree initialization code
2022-06-13 02:50:00 【Sleepy snail】

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
TreeNode* initBTree(int elements[], int size)
{
if (size < 1)
{
return NULL;
}
cout << " Start to create " << endl;
// Dynamic applications size Pointer array of size
TreeNode** nodes = new TreeNode * [size];
// take int Data to TreeNode node
for (int i = 0; i < size; i++)
{
if (elements[i] == 0)
{
nodes[i] = nullptr;
}
else
{
nodes[i] = new TreeNode(elements[i]);
}
}
queue<TreeNode*> nodeQueue;
nodeQueue.push(nodes[0]);
TreeNode* node;
int index = 1;
while (index < size)
{
/* node = nodeQueue.front();
nodeQueue.pop();
nodeQueue.push(nodes[index++]);
node->left = nodeQueue.back();
nodeQueue.push(nodes[index++]);
node->right = nodeQueue.back();*/
node = nodeQueue.front();
nodeQueue.pop();
TreeNode* lnode= nodes[index++];
if(lnode!=nullptr) nodeQueue.push(lnode);
node->left = lnode;
TreeNode* rnode = nodes[index++];
if (rnode != nullptr) nodeQueue.push(rnode);
node->right = rnode;
}
return nodes[0];
}
边栏推荐
- Opencvshare4 and vs2019 configuration
- Opencv 07, pixel read, change and bitmap write
- Linked list: the entry node of the link in the linked list
- OpenCVSharpSample04WinForms
- FFmpeg原理
- 03 认识第一个view组件
- [reading papers] deep learning face representation from predicting 10000 classes. deepID
- nn. Conv2d and nn Convtranspose2d differences
- Ijkplayer source code - setting options
- 2019 - sorting out the latest and most comprehensive IOS test questions (including framework and algorithm questions)
猜你喜欢

Uni app Foundation

微信云开发粗糙理解
![[reading point paper] yolo9000:better, faster, stronger, (yolov2), integrating various methods to improve the idea of map and wordtree data fusion](/img/b7/0171a637d00e979fb92b82d0c038be.jpg)
[reading point paper] yolo9000:better, faster, stronger, (yolov2), integrating various methods to improve the idea of map and wordtree data fusion
![[data analysis and visualization] key points of data drawing 6- too many data groups](/img/68/564e289a2fa54d65437877d16faefb.jpg)
[data analysis and visualization] key points of data drawing 6- too many data groups

智能安全配电装置如何减少电气火灾事故的发生?

Professional database management software: Valentina Studio Pro for Mac

Detailed explanation of UCI datasets and their data processing (with 148 datasets and processing codes attached)
![HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)](/img/c9/884aa008a185a471dfe252c0756fc1.png)
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)

Linked list: palindrome linked list

Principle and steps of principal component analysis (PCA)
随机推荐
Matlab: find the inner angle of n-sided concave polygon
Linked list: palindrome linked list
[data and Analysis Visualization] data operation in D3 tutorial 3-d3
Prometheus node_exporter安装并注册为服务
[data analysis and visualization] key points of data mapping 7- over mapping
Radio design and implementation in IVI system
冲刺强基计划数学物理专题一
too old resource version,Code:410
Ijkplayer source code - audio playback
01 初识微信小程序
Linked list: reverse linked list
[data analysis and visualization] key points of data drawing 8- use of circular bar chart
redis. Conf general configuration details
Delphi implements adding a column of serial number to the CXGRID list
[life science] DNA extraction of basic biological experiments
[dest0g3 520 orientation] dest0g3, which has been a long time since we got WP_ heap
Ijkplayer source code ---setdatasource
Collection of IOS development interview and underlying learning videos
Use of OpenCV 11 kmeans clustering
04路由跳转并携带参数