pku 2255 二叉树的三种遍历

深藏阁楼爱情的钟 2022-08-13 03:30 257阅读 0赞

#include #include #include using namespace std; string s1, s2; typedef struct TNode { char data; struct TNode *lchild, *rchild; }*BTree; void build(BTree& T, int start1, int end1, int start2, int end2) { if(start1 > end1) { T = NULL; return; } else { T = new TNode; T->data = s1[start1]; int index = s2.find(s1[start1], start2); build(T->lchild, start1+1, start1+index-start2, start2, index-1); build(T->rchild, start1+index-start2 +1, start1+index-start2+end2-index, index+1, end2); } } void postVisit(BTree T) { if(T) { postVisit(T->lchild); postVisit(T->rchild); cout << T->data; } } int main() { BTree T; while(cin >> s1 >> s2) { build(T , 0, s1.size()-1, 0, s2.size()-1); postVisit(T); cout << endl; T = NULL; } return 0; }

发表评论

表情:
评论列表 (有 0 条评论,257人围观)

还没有评论,来说两句吧...

相关阅读

    相关 顺序】

    1、中序遍历 指对树中任意节点的访问是在遍历完其左子树后进行的,访问此节点后,再对其右子树遍历(左根右)。遍历从根节点开始,遇到每个节点时,其遍历过程为: 中序遍

    相关

    二叉树的遍历分为以下三种: 先序遍历:遍历顺序规则为【根左右】 中序遍历:遍历顺序规则为【左根右】 后序遍历:遍历顺序规则为【左右根】 什么是【根左右】?就是先遍历根,

    相关 问题

    1、先序遍历:【根左右】 ![70][] 所谓【根左右】是指先遍历根节点,然后左孩子节点,最后右孩子节点。 所以,上图的遍历顺序是:ABCDEF 2、中序遍历:【

    相关 口诀

    二叉树的三种遍历口诀 最近在准备笔试面试题,复习复习数据结构相关知识,在二叉树这边好多都忘了,所以特地写下来,防止以后忘了可以迅速查找 1.前序遍历:根节点—-左子树—