最近刷题的时候发现要用到树的结构,当时脑子就宕机咯,不知道怎么建立一个自己需要的树,这篇博文介绍了如何建立一个二叉树。
不同于剑指offer或者书上讲的给定先序遍历的数组和中序遍历的数组来唯一确定一棵树,我给出的是已知二叉树的结构在自己的白纸上,怎么建立它。
已知一颗二叉树在白纸上,我的习惯是把它变成一个二维数组(也就相当于层序遍历,但是在结点为null的位置插入0),之后依据这个二维的数组建立一个对应的二叉树。下面是代码。
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Tree{
int val;
Tree* left;
Tree* right;
Tree(int a):val(a),left(nullptr),right(nullptr){}
};
void TreeConstruct(vector<vector<int> >& arr,Tree* root,int i,int j)
{
if(arr[i+1][j*2]==0)
root->left = nullptr;
else
{
Tree* nodel = new Tree(arr[i+1][j*2]);
root->left = nodel;
}
if(arr[i+1][j*2+1]==0)
root->right = nullptr;
else
{
Tree* noder = new Tree(arr[i+1][j*2+1]);
root->right = noder;
}
if(i < arr.size()-2)
{
if(root->left != nullptr)
TreeConstruct(arr,root->left,i+1,j*2);
if(root->right != nullptr)
TreeConstruct(arr,root->right,i+1,j*2+1);
}
}
void inorder(Tree* root)
{
if(root == nullptr)
return;
cout << root->val << " ";
inorder(root->left);
inorder(root->right);
}
int main(){
vector<vector<int> > arr = {{1},{2,3},{4,0,6,7},{1,2,0,0,5,5,3,2}};
Tree* root = new Tree(arr[0][0]);
TreeConstruct(arr,root,0,0);
// for(int i = 0;i < arr.size();i++)
// {
// for(auto x : arr[i])
// {
// cout << x << " ";
// }
// cout << endl;
// }
inorder(root1);
}
第二种方式是给出一个 层序遍历的queue来创建,下面是两种方式混在一下的完整代码。代码都经过测试的。
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Tree{
int val;
Tree* left;
Tree* right;
Tree(int a):val(a),left(nullptr),right(nullptr){}
};
void TreeConstruct(Tree* root,queue<int> &arr)
{
queue<Tree*> que;
que.push(root);
while(!arr.empty() && !que.empty())
{
Tree *node = que.front();
que.pop();
Tree* nodel = new Tree(arr.front());
arr.pop();
Tree* noder = new Tree(arr.front());
arr.pop();
node->left = nodel;
node->right = noder;
que.push(node->left);
que.push(node->right);
}
}
void TreeConstruct(vector<vector<int> >& arr,Tree* root,int i,int j)
{
if(arr[i+1][j*2]==0)
root->left = nullptr;
else
{
Tree* nodel = new Tree(arr[i+1][j*2]);
root->left = nodel;
}
if(arr[i+1][j*2+1]==0)
root->right = nullptr;
else
{
Tree* noder = new Tree(arr[i+1][j*2+1]);
root->right = noder;
}
if(i < arr.size()-2)
{
if(root->left != nullptr)
TreeConstruct(arr,root->left,i+1,j*2);
if(root->right != nullptr)
TreeConstruct(arr,root->right,i+1,j*2+1);
}
}
void inorder(Tree* root)
{
if(root == nullptr)
return;
cout << root->val << " ";
inorder(root->left);
inorder(root->right);
}
void BreadthFirstSearch(Tree *root)
{
queue<Tree*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty())
{
Tree *node = nodeQueue.front();
cout << node->val << ' ';
nodeQueue.pop();
if (node->left)
{
nodeQueue.push(node->left);
}
if (node->right)
{
nodeQueue.push(node->right);
}
}
}
int main(){
vector<vector<int> > arr = {{1},{2,3},{4,0,6,7},{1,2,0,0,5,5,3,2}};
vector<int> helpque = {1,2,0,4,5,6,7};
queue<int> arr1;
for(auto x : helpque)
{
arr1.push(x);
}
Tree* root = new Tree(arr[0][0]);
TreeConstruct(arr,root,0,0);
Tree* root1 = new Tree(arr1.front());
arr1.pop();
TreeConstruct(root1,arr1);
// for(int i = 0;i < arr.size();i++)
// {
// for(auto x : arr[i])
// {
// cout << x << " ";
// }
// cout << endl;
// }
BreadthFirstSearch(root1);
inorder(root1);
}