定义
struct node{
int data;
int lchild;
int rchild;
}nodes;
替换值
void search(int root,int x,int y){
if(root==-1)
return;
if(nodes[root].data==x){
nodes[root].data=y;
}
search(nodes[root].lchild,x,y);
search(nodes[root].rchild,x,y);
}
深度遍历
void preorder(int root){
if(root==-1)
return;
cout<<nodes[root].data;
preorder(nodes[root].lchild);
preorder(nodes[root].rchild);
}
广度遍历
void layerorder(int root){
queue<int> q;
q.push(nodes[root].data);
while(!q.empty()){
int front=q.front();
q.pop();
cout<<nodes[front].data<<endl;
if(nodes[front].lchild!=-1)
q.push(nodes[front].lchild);
if(nodes[front].rchild!=-1)
q.push(nodes[front].rchild);
}
}