二叉树的建立与层序遍历
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
int data;
node* lchild;
node* rchild;
};
node* newNode(int val)
{
node* Node = new node;
node->data = val;
node->lchild = node->rchild = NULL;
return Node;
}
node* search(node* root,int val)
{
if(root==NULL) return NULL;
if(root->data==val) return root;
search(root->left,val);
search(root->right,val);
}
node* insert(node* &root,int x)
{
if(root==NULL)
{
root = newNode(x);
return ;
}
if()
{
insert(root->lchild,x);
}
else
{
insert(root->rchild,x);
}
}
node *Create(int data[],int n)
{
node* root = NULL;
for(int i = 0;i<n;i++)
{
insert(root,data[i]);
}
return root;
}
void LayerOrder(node* root)
{
queue<node*>q;
q.push(root);
while(q.size())
{
node *now = q.front();
q.pop();
cout<<now->data<<' ';
if(now->lchild!=NULL) q.push(now->lchild);
if(now->rchild!=NULL) q.push(now->rchild);
}
}
int main()
{
node* root = NULL;
}
不错!