首页 > 编程知识 正文

二叉树前序中序后序图,前序遍历后序遍历确定二叉树

时间:2023-05-06 10:00:12 阅读:30042 作者:1409

通过二叉树的前相遍历和中相遍历确定后续遍历

输入格式:

第1行:扫描的排列长度

遍历第2行:前

第3行:中依次扫描

输出格式:

输出后的横移

输入样本:

8GDAFEMHZADEFGHMZ输出示例:

AEFDHZMG模型图:

节点- elem=* pre; //父for(introotindex=0; rootIndexlength; rootIndex () if(in[rootindex]==*pre ) )//通过顺序扫描找到中顺序扫描根节点的位置break; }节点左=paitp (pre1,in,rootIndex ); //左节点右=paitp (prerootindex 1,in rootIndex 1,length-rootIndex-1 ); //右边的想法是以下:

1 .遍历的方法

首先是:瓶-左-右

中顺扫描:左-根-右

后遍历:左-右-根

2 .按前序找到根节点,在根节点中将中序遍历分成左/右

3 .重复2直到数组长度为0

# includeiostreamusingnamespacestd; //将二叉树元素的类型定义为inttypedef char type的struct treenode { struct treenode * left; //左struct TreeNode* right; //右类型elem; //父亲}; Treenode*paitp(consttype*pre,const type* in,int length ) if ) length==0) return 0; TreeNode* node=new TreeNode; int根索引=0; 节点- elem=* pre; //父亲for (; rootIndexlength; rootIndex () if(in[rootindex]==*pre ) )//通过顺序扫描找到中顺序扫描根节点的位置break; }节点左=paitp (pre1,in,rootIndex ); //左节点右=paitp (prerootindex 1,in rootIndex 1,length-rootIndex-1 ); //右coutnode-elem; //根节点返回节点; (}int main ) ) {int n; cinn; 类型in[n],in[n]; for(intI=0; in; I ) cinpr[i]; for(intI=0; in; I ) cinin[i]; paitp(pr,in,n ); 返回0; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。