二叉树的重建
发布日期:2021-09-20 08:56:06
浏览次数:15
分类:技术文章
本文共 2541 字,大约阅读时间需要 8 分钟。
文章目录
1.根据二叉树的中序和后续遍历重建二叉树
思路:假设递归过程中,某一步的后序序列区间为[postL,postR],中序序列区间为[inL,inR];
1. 根据后序遍历的特点可知,postR位置为根结点;2. 从中序序列中,寻找出root的位置k,k左边的均为左子树,右边的均为右子树;3. 将左子树区间[postL,k-1]作为新的后序和中序序列,进行递归。
代码如下:
#include#include #include using namespace std;struct BTNode { BTNode(char data):_data(data),_pLeft(nullptr),_pRight(nullptr){ } char _data; BTNode* _pLeft; BTNode* _pRight;};void PreOrder(BTNode* pRoot) { if (pRoot == nullptr) return; cout << pRoot->_data << " "; PreOrder(pRoot->_pLeft); PreOrder(pRoot->_pRight);}void PostOrder(BTNode* pRoot) { if (pRoot == nullptr) return; PostOrder(pRoot->_pLeft); PostOrder(pRoot->_pRight); cout << pRoot->_data << " ";}BTNode* ReBuildBTTree(string& Post, string& In, int PostL, int PostR, int InL, int InR) { if (PostL > PostR) return nullptr; BTNode* pRoot = new BTNode(Post[PostR]); int k = 0; while (In[k] != pRoot->_data) { ++k; } int numLeft = k - InL; pRoot->_pLeft = ReBuildBTTree(Post, In, PostL, PostL + numLeft - 1, InL, k - 1); pRoot->_pRight = ReBuildBTTree(Post, In, PostL + numLeft, PostR - 1, k + 1, InR); return pRoot;}int main() { string In = "dgbaechf"; string Post = "gbdehfca"; string Pre = "adbgcefh"; BTNode* pNewRoot = ReBuildBTTree(Post, In, 0, 7, 0, 7); PreOrder(pNewRoot); cout << endl; system("pause"); return 0;}
输出结果:
a d b g c e f h
2.根据二叉树的中序和前序遍历重建二叉树
思路:假设递归过程中,某一步的后序序列区间为[preL,preR],中序序列区间为[inL,inR];
1. 根据前序遍历的特点可知,postL位置为根结点;2. 从中序序列中,寻找出root的位置k,k左边的均为左子树,右边的均为右子树;3. 将左子树区间[preL+1,preL+numLeft]作为新的后序和中序序列,进行递归。
代码如下:
#include#include #include using namespace std;struct BTNode { BTNode(char data):_data(data),_pLeft(nullptr),_pRight(nullptr){ } char _data; BTNode* _pLeft; BTNode* _pRight;};void PostOrder(BTNode* pRoot) { if (pRoot == nullptr) return; PostOrder(pRoot->_pLeft); PostOrder(pRoot->_pRight); cout << pRoot->_data << " ";}BTNode* Create(string& Pre, string& In, int preL, int preR, int InL, int InR) { if (preL > preR) return nullptr; BTNode* pRoot = new BTNode(Pre[preL]); int k = 0; while (In[k] != pRoot->_data) { ++k; } int numLeft = k - InL; pRoot->_pLeft = Create(Pre, In, preL + 1, preL + numLeft, InL, k - 1); pRoot->_pRight = Create(Pre, In, preL + numLeft + 1, preR, k + 1, InR); return pRoot;}int main() { string In = "dgbaechf"; string Post = "gbdehfca"; string Pre = "adbgcefh"; BTNode* pNew2 = Create(Pre, In, 0, 7, 0, 7); PostOrder(pNew2); cout << endl; system("pause"); return 0;}
输出结果:
g b d e h f c a
转载地址:https://blog.csdn.net/a_hang_szz/article/details/100848140 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月09日 02时04分30秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
LeetCode题解(1154):判断日期在一年中的第几天(Python)
2019-04-26
LeetCode题解(1160):判断可由指定字母拼写的所有单词总长(Python)
2019-04-26
LeetCode题解(1170):比较字符串最小字母的出现频次(Python)
2019-04-26
LeetCode题解(1175):质数排列(Python)
2019-04-26
LeetCode题解(1179):重新格式化部门表(SQL)
2019-04-26
LeetCode题解(1184):公交站间的距离(Python)
2019-04-26
LeetCode题解(1422):分割字符串的最大得分(Python)
2019-04-26
LeetCode题解(1436):旅行终点站-寻找循环的终点(Python)
2019-04-26
H5+CSS前端特效源代码:可旋转动态日文片假名
2019-04-26
python程序没有报错但是运行没有任何结果怎么办?
2019-04-26
简单说一说MySQL中drop、delete与truncate的区别?
2019-04-26
InnoDB与MyISAM的区别
2019-04-26
思科 Packet Tracer 实验六 RIP路由协议基本配置
2019-04-26
计算机网络实验七:DHCP基本配置
2019-04-26
计算机网络实验八:思科NAT的基本配置
2019-04-26
三郎数据结构算法学习笔记:单向环形链表约瑟夫问题
2019-04-26
前端特效H5+css+js:动态可拉进度条+附完整源码
2019-04-26
三郎数据结构学习笔记:双向循环链表(判断是否对称)附源码
2019-04-26
三郎数据结构算法学习笔记:基数排序
2019-04-26
三郎数据结构算法学习笔记:斐波那契(黄金分割法)查找算法
2019-04-26