当前位置:网站首页>二叉树初始化代码
二叉树初始化代码
2022-06-13 02:48:00 【贪睡的蜗牛】

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 << "开始创建" << endl;
//动态申请size大小的指针数组
TreeNode** nodes = new TreeNode * [size];
//将int数据转换为TreeNode节点
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];
}
边栏推荐
- 智能安全配电装置如何减少电气火灾事故的发生?
- Find the number of permutations
- nn. Conv2d and nn Convtranspose2d differences
- Opencvshare4 and vs2019 configuration
- [reading papers] deep learning face representation from predicting 10000 classes. deepID
- vant实现移动端的适配
- 如何挑选基金产品?什么样的基金是好基金?
- Simple use of leaflet - offline map scheme
- 在IDEA使用C3P0連接池連接SQL數據庫後卻不能顯示數據庫內容
- FFmpeg原理
猜你喜欢

Image classification system based on support vector machine (Matlab GUI interface version)

Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac

Useful websites for writing papers and studying at ordinary times
![[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
![[life science] DNA extraction of basic biological experiments](/img/84/c1968c2c08feab44b14a529420eea9.jpg)
[life science] DNA extraction of basic biological experiments

专业的数据库管理软件:Valentina Studio Pro for Mac

冲刺强基计划数学物理专题一

Rough understanding of wechat cloud development

Prometheus install and register services

数仓笔记|针对客户维度建模需要关注的5个因素
随机推荐
Data processing in detailed machine learning (II) -- Feature Normalization
redis. Conf general configuration details
Prometheus安装并注册服务
05 tabbar navigation bar function
Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx
House raiding
Introduction and download of common data sets for in-depth learning (with network disk link)
js 解构赋值
Leetcode 926. Flip string to monotonically increasing [prefix and]
How to destroy a fragment- How to destroy Fragment?
[data analysis and visualization] key points of data drawing 6- too many data groups
Introduction to facial expression recognition system -- offline environment configuration
Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
数字IC设计——FIFO的设计
[data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3
02 optimize the default structure of wechat developer tools
MySQL index
Opencvshare4 and vs2019 configuration
Matlab: obtain the figure edge contour and divide the figure n equally
Modify the color of El input, textarea and El checkbox when they are disabled