包括前序遍历、中序遍历、后序遍历、层次遍历
根据前序和中序构建二叉树、根据中序和后序构建二叉树、根据中序和层次构建二叉树。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
struct node{
int data;
// int level;
node* left;
node* right;
};
void preorder(node* root){//前序遍历
if(root==NULL){
return;//空树
}
printf("%d ",root->data);//当前节点;
preorder(root->left);
preorder(root->right);
}
void inorder(node* root){//中序遍历
if(root==NULL){
return;
}
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
void postorder(node *root){//后序遍历
if(root==NULL){
return;
}
postorder(root->left);
postorder(root->right);
printf(&