当前位置:网站首页>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];
}
边栏推荐
- Data processing in detailed machine learning (II) -- Feature Normalization
- Ijkplayer source code --- decode
- Linked list: reverse linked list
- Rounding in JS
- Introduction to facial expression recognition system -- offline environment configuration
- Pycharm and Anaconda ultra detailed installation and configuration tutorial
- Introduction to facial expression recognition system - Technical Paper Edition
- Hash table: valid anagrams
- Sword finger offer2: queue summary
- Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
猜你喜欢

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

Branch and bound method, example sorting

OneNote使用指南(一)

Graduation project - campus old thing recycling system based on stm32

Summary of the latest IOS interview questions in June 2020 (answers)

A real-time target detection model Yolo

Introduction and download of common data sets for in-depth learning (with network disk link)

Prometheus node_exporter安装并注册为服务

05 tabBar导航栏功能

Principle and steps of principal component analysis (PCA)
随机推荐
[deep learning] fast Reid tutorial
redis. Conf general configuration details
[reading papers] comparison of deeplobv1-v3 series, brief review
05 tabbar navigation bar function
OpenCVSharpSample05Wpf
Multiple knapsack problem
Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
Hash table: valid anagrams
Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial
Rounding in JS
Opencvsharp4 pixel read / write and memory structure of color image and gray image
A real-time target detection model Yolo
[data analysis and visualization] key points of data drawing 6- too many data groups
Ijkplayer source code - remuxing
The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)
Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示
How can intelligent safe power distribution devices reduce the occurrence of electrical fire accidents?
Model prediction of semantic segmentation
Image classification system based on support vector machine (Matlab GUI interface version)
[reading papers] visual convolution zfnet