当前位置:网站首页>二叉树初始化代码
二叉树初始化代码
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];
}
边栏推荐
- Image classification system based on support vector machine (Matlab GUI interface version)
- Ijkplayer source code ---setdatasource
- Model prediction of semantic segmentation
- How did you spend your winter vacation perfectly?
- Hash tables: metaphrases
- How to destroy a fragment- How to destroy Fragment?
- Linked lists: rearranging linked lists
- Data warehouse notes | 5 factors that need attention for customer dimension modeling
- wx.createSelectorQuery()在components获取Dom节点的使用
- 01 initial knowledge of wechat applet
猜你喜欢

03 认识第一个view组件

05 tabbar navigation bar function
![[data analysis and visualization] key points of data drawing 8- use of circular bar chart](/img/1f/232f2f0134867eeec3f1cfe005b2b5.jpg)
[data analysis and visualization] key points of data drawing 8- use of circular bar chart

Huffman tree and its application

A wechat app for shopping

Mp4 playback
![HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)](/img/c9/884aa008a185a471dfe252c0756fc1.png)
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)

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

The weight of the input and textarea components of the applet is higher than that of the fixed Z-index

05 tabBar导航栏功能
随机推荐
How to select fund products? What kind of fund is a good fund?
Find the number of permutations
Graduation project - campus old thing recycling system based on stm32
Opencv 07, pixel read, change and bitmap write
A real-time target detection model Yolo
Hash table: the time complexity of insert, delete and random access is O (1)
redis.conf总配置详解
02 优化微信开发者工具默认的结构
JS multiple judgment writing
Example 4 linear filtering and built-in filtering
Traverse the array and delete an element until it is deleted
too old resource version,Code:410
Ijkplayer source code -- mnatemediaplayer of ijkmediaplayer
The weight of the input and textarea components of the applet is higher than that of the fixed Z-index
Linked list: the first coincident node of two linked lists
Linked lists: rearranging linked lists
Hash table: whether alien languages are sorted
Ijkplayer source code ---setdatasource
Opencv 9 resize size change rotate rotate blur mean (blur)
小程序 input,textarea组件权重比fixed的z-index都高