当前位置:网站首页>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];
}
边栏推荐
- Change the topic of change tax
- JS deconstruction assignment
- Radio design and implementation in IVI system
- [data analysis and visualization] key points of data drawing 8- use of circular bar chart
- Ijkplayer source code ---setdatasource
- Node uses post to request req Pit with empty body
- How to destroy a fragment- How to destroy Fragment?
- Advanced stair climbing
- Vant realizes the adaptation of mobile terminal
- House raiding
猜你喜欢

Linked lists: rearranging linked lists

js 解构赋值

小程序 input,textarea组件权重比fixed的z-index都高
![[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2](/img/a1/151d2afe6d7f0bd95fe93fc80f633e.jpg)
[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2

How did you spend your winter vacation perfectly?

数仓笔记|针对客户维度建模需要关注的5个因素

04路由跳转并携带参数

The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)

Opencvsharp4 handwriting recognition

Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
随机推荐
Delphi implements adding a column of serial number to the CXGRID list
Use of OpenCV 12 findcircuits and drawcircuits
How did you spend your winter vacation perfectly?
Hash table: least recently used cache
Use of OpenCV 11 kmeans clustering
Digital IC Design -- FIFO design
[data analysis and visualization] key points of data drawing 4- problems of pie chart
Collection of IOS development interview and underlying learning videos
Opencvsharp4 pixel read / write and memory structure of color image and gray image
CDN single page reference of indexbar index column in vant framework cannot be displayed normally
04 route jump and carry parameters
MySQL index
02 优化微信开发者工具默认的结构
Svg filter effect use
Linked lists: rearranging linked lists
Ijkplayer source code --- decode
House raiding
Professional database management software: Valentina Studio Pro for Mac
Opencvshare4 and vs2019 configuration
OneNote User Guide (1)