ipqtjmqj 2020-02-14
void preorder(node* root){ if(root == NULL){ return; } //访问根结点root, 例如将其数据域输出 printf("%d\n", root->data); //访问左子树 preorder(root->lchild); //访问右子树 preorder(root->rchild); }
void inorder(node* root){ if(root == NULL){ return; } inorder(root->lchild); printf("%d\n", root->data); inorder(root->rchild); }
void postorder(node* root){ if(root == NULL){ return; } postorder(root->lchild); postorder(root->rchild); printf("%d\n", root->data); }
void LayerOrder(node* root){ queue<node*> q; q.push(root); while(!q.empty()){ node* now = q.front(); q.pop(); printf("%d\n", now->data); if(now->lchild != NULL) q.push(now->lchild); if(now->rchild != NULL) q.push(now->rchild); } }
struct node{ int data; int layer; node* lchild; node* rchild; } void LayerOrder(node* root){ queue<node*> q; root->layer = 1; q.push(root); while(!q.empty()){ node* now = q.front(); q.pop(); printf("%d\n", now->data); if(now->lchild != NULL){ now->lchild->layer = now->layer+1; q.push(now->lchild); } if(now->rchild != NULL){ now->rchild->layer = now->layer+1; q.push(now->rchild); } } }
node* create(int preL, int preR, int inL, int inR){ if(preL > preR){ return NULL;//先序序列长度小于等于0时,直接返回 } node* root = new node;//新建一个结点,用于存放二叉树的根结点 //如果是先序和中序那么这里就是从左边第一个结点就是根结点所以为preL,而后序和中序是从后面第一个是根结点所以为postR root->data = pre[preL]/post[postR];//新结点的数据域为根结点的值 int k; for(k = inL; k <= inR; k++){ if(in[k] == pre[preL]){ break; } } int numLeft = k - inL;//左子树的结点个数 //左子树的先序区间为[preL+1, preL+numLeft], 中序区间[inL, k-1] //返回左子树的根结点地址,赋值给root的左指针 root->lchild = create(preL+1/postL, preL+numLeft/postL+numLeft-1, inL, k-1); //右子树的先序区间[preL+numLeft+1, preR], 中序区间为[k+1, inR]; //返回右子树的根结点地址,赋值给root的右指针 root->rchild = create(preL+numLeft+1/postL+numLeft, preR/postR-1, k+1, inR); return root;//返回根结点地址 }