二叉树的操作之由中根和后跟序列重建二叉树
2018-04-06 12:00
288 查看
#include <iostream> #include <vector> #include <string> #include <malloc.h> #include <stdio.h> #include <string.h> using namespace std; typedef struct node { int data; node *lchild,*rchild; }TreeNode,* BTree; void Pre(BTree root) { if(root==NULL) return ; else { printf("%d ",root->data); Pre(root->lchild); Pre(root->rchild); } } BTree ReBuild(int *a,int *b,int n) { BTree root; root = (BTree)malloc(sizeof(TreeNode));//注意一定要先创建一个实例 root->lchild=NULL; root->rchild=NULL; if(n>=1) { root->data=b[n-1]; int i; for(i=0;i<n&&a[i]!=b[n-1];i++); root->lchild=ReBuild(a,b,i); root->rchild=ReBuild(a+i+1,b+i,n-i-1); return root;//迭代出口 } else return NULL;//迭代出口 } int a[10000]; int b[10000]; int main() { char c; int i=0,j=0; int n; while (cin >> n){ a[i++]=n; c = cin.get(); if (c == '\n'){ break; } } while (cin >> n){ b[j++]=n; c = cin.get(); if (c == '\n') break; } BTree bt; bt=ReBuild(a,b,i); Pre(bt); printf("\n"); return 0; }
相关文章推荐
- 利用中根序列和后根序列重建二叉树
- 序列节点根据前中后序和层序重建二叉树(PAT1020、PAT1043)
- 根据先序序列和中序序列重建二叉树
- Java实现由前序序列和中序序列重建二叉树,并后序输出。
- 由前序遍历序列和中序遍历序列重建二叉树
- 已知二叉树的前序与中序遍历序列,求重建后的二叉树(即是二叉树的重建)
- 【二叉树】已知二叉树前序序列和中序序列,重建唯一二叉树
- 面试题-前序中序序列重建二叉树
- [LeetCode] 根据中序和后序序列重建二叉树
- 根据前序遍历序列和中序遍历序列重建二叉树
- 由中根序列和后根序列重建二叉树
- 【二叉树】已知二叉树前序序列和中序序列,重建唯一二叉树
- 根据前序和中序序列重建二叉树
- 设二叉树结点的先根序列、中根序列和后根序列中,所有叶子结点的先后顺序____。
- 前/中序遍历序列重建二叉树
- 每天一个算法之根据前序中序序列重建二叉树
- 剑指:根据输入的前序和中续遍历序列重建二叉树
- 二叉树重建 ( 根据中序序列和前序序列获取后序序列)
- 根据前序遍历序列和中序遍历序列重建二叉树
- 根据中序和先序重建二叉树+二叉树的镜像+二叉树的深度+判断后序序列+判断子树