剑指offer刷题之c++实现的根据二叉树的前序和中序遍历重建二叉树
来源:互联网 发布:饮冰室文集 知乎 编辑:程序博客网 时间:2024/04/29 04:52
#include "myHead.h"#include "allConstructBinaryTree.cpp" /*利用前序和后序遍历,构造二叉树。前序第一个节点就是根节点,然后根据这个节点去后序中找到这个数,并记下这个数的下标作为划分的标准。这个数划分了两个范围。左边一拨全是左子树,右边一拨全是右子树。 可以统计左子树的个数,下一次,在前序中去掉第一个再开始,以这个中序序列的第二个数为左子树的根,继续在中序的左子树一拨中划分左右子树。知道叶子节点。如果划分后序的左子树个数为零,那么只有右子树,如果划分后序的右子树个数为零,那么只有左子树。 */ void create2(TreeNode *&T,vector<int> pre,vector<int> in,int l1,int h1,int l2,int h2);struct TreeNode* reConstructBinaryTreeByPreIn(vector<int> pre,vector<int> in) {int l1,h1,l2,h2;TreeNode *T; l1 = 0; h1 = pre.size()-1; l2 = 0; h2 = in.size()-1; printf("first: l1=%d,h1=%d,l2=%d,h2=%d",l1,h1,l2,h2);create2(T,pre,in,l1,h1,l2,h2); inOrder(T);cout<<"return "<<endl; return T; } void create2(TreeNode *&T,vector<int> pre,vector<int> in,int l1,int h1,int l2,int h2){int j=0,l=0;printf("\tl1=%d,h1=%d,l2=%d,h2=%d\n",l1,h1,l2,h2);T = (TreeNode *) malloc(sizeof(TreeNode)) ;T->val = pre[l1];cout<<"pre2:"<<T->val<<endl; for(j = l2;j<=h2;j++){ if(pre[l1] == in[j]){ printf("j=%d",j); break; } } l = j-l2;if(l2 == j){cout<<"left null"; T->left = NULL;}else create2(T->left,pre,in,l1+1,l1+l,l2,j-1); if(h2 == j){ cout<<"right null"; T->right = NULL; } else create2(T->right,pre,in,l1+l+1,h1,j+1,h2); } int main(){ vector<int> pre,pre1;vector<int> in;vector<int> post,post1;pre.push_back(1);pre.push_back(2); pre.push_back(4);pre.push_back(7);pre.push_back(3);pre.push_back(5);pre.push_back(6);pre.push_back(8);in.push_back(4);in.push_back(7);in.push_back(2);in.push_back(1);in.push_back(5);in.push_back(3);in.push_back(8);in.push_back(6); cout<<"pre: ";for(vector<int>::iterator it=pre.begin();it<pre.end();++it){ cout<<*it<<" ";}cout<<endl;cout<<"in: ";for(vector<int>::iterator it=in.begin();it<in.end();++it){ cout<<*it<<" ";}cout<<endl;TreeNode *t =reConstructBinaryTreeByPreIn(pre,in);cout<<endl<<"finally"; inOrder(t);return 1;}
拓展:根据严格二叉树的前序和后序遍历重建二叉树
#include "myHead.h"#include "allConstructBinaryTree.cpp" /*利用前序和中序,其中给定的序列的数都不重复,从而构造一颗严格二叉树,所谓严格二叉树每颗父节点要不都没有左右孩子 ,要不左右子树都不空但是,显然这个限制条件就不能在普通二叉树中使用了 */void create(TreeNode *&T,vector<int> pre,vector<int> in,int l1,int h1,int l2,int h2);struct TreeNode* reConstructBinaryTreeByPrePostInConditions(vector<int> pre,vector<int> in) {int l1,h1,l2,h2;TreeNode *T; l1 = 0; h1 = pre.size()-1; l2 = 0; h2 = in.size()-1; printf("first: l1=%d,h1=%d,l2=%d,h2=%d",l1,h1,l2,h2); create(T,pre,in,l1,h1,l2,h2); return T; } void create(TreeNode *&T,vector<int> pre,vector<int> in,int l1,int h1,int l2,int h2){ int i = 0,j=0,l=0;printf("l1=%d,h1=%d,l2=%d,h2=%d\n",l1,h1,l2,h2);if(l1<= h1) {T = (TreeNode *) malloc(sizeof(TreeNode)) ;T->val = pre[l1]; if(l1 == h1){ T->left = NULL; T->right = NULL; } else{ for(j = l2;j<=h2;j++){ i++; if(pre[l1+1] == in[j]){ break; } } i--;l = j-l2+1; create(T->left,pre,in,l1+1,l1+l,l2,j); create(T->right,pre,in,l1+l+1,h1,j+1,h2-1); } } } int main(){ vector<int> pre1;vector<int> post1; pre1.push_back(1); pre1.push_back(2); pre1.push_back(4); pre1.push_back(5); pre1.push_back(3); pre1.push_back(6); pre1.push_back(8); pre1.push_back(9); pre1.push_back(7); pre1.push_back(10); pre1.push_back(11); pre1.push_back(12); pre1.push_back(13); post1.push_back(4); post1.push_back(5); post1.push_back(2); post1.push_back(8); post1.push_back(9); post1.push_back(6); post1.push_back(10); post1.push_back(12); post1.push_back(13); post1.push_back(11); post1.push_back(7); post1.push_back(3); post1.push_back(1); cout<<"pre1: ";for(vector<int>::iterator it=pre1.begin();it<pre1.end();++it){ cout<<*it<<" ";}cout<<endl;cout<<"post1: ";for(vector<int>::iterator it=post1.begin();it<post1.end();++it){ cout<<*it<<" ";}cout<<endl;TreeNode *t =reConstructBinaryTreeByPrePostInConditions(pre1,post1);cout<<endl<<"finally:"<<endl; inOrder(t);return 1;}
0 0
- 剑指offer刷题之c++实现的根据二叉树的前序和中序遍历重建二叉树
- 剑指Offer-根据二叉树的前序和后序遍历重建二叉树
- (剑指offer笔记)根据前序遍历和中序遍历重建二叉树
- 根据前序遍历和中序遍历重建二叉树的Java实现
- 根据二叉树的前序遍历和中序遍历的结果,重建二叉树
- 根据二叉树的前序遍历和中序遍历重建二叉树
- 【面试题】剑指Offer-6-根据前序和中序遍历重建二叉树
- 码农小汪-剑指Offer之7 - 重建二叉树(根据前序遍历中序遍历求二叉树)
- 剑指offer--重建二叉树[根据二叉树的先序、中序遍历结果重建二叉树]
- 剑指:根据输入的前序和中续遍历序列重建二叉树
- 剑指offer 重建二叉树-前序遍历和中序遍历重建
- 根据中序和前序遍历重建二叉树
- 根据前序和中序遍历重建二叉树
- 根据二叉树的前序遍历和中序遍历的结果,请重建出该二叉树
- 根据二叉树的前序遍历和中序遍历结果重建出该二叉树
- 已知二叉树的前序遍历和中序遍历重建二叉树(二叉树)
- 根据前序和中序的遍历序列重建二叉树
- 重建二叉树---根据前序和中序遍历结果重建二叉树
- 如下为类型CMyString的声明,请为该类型添加赋值运算符函数
- uva 437 动态规划
- HDU 5363 Key Set
- OC循环渐进:KVC的其它操作
- Android有效解决加载大图片时内存溢出的问题
- 剑指offer刷题之c++实现的根据二叉树的前序和中序遍历重建二叉树
- 网络协议知识
- [机房合作]—SqlHelper我们又约了
- UE3 中文教程主页
- 【BOM操作】JavaScript中的event对象之总结
- usb驱动
- Android 内存泄漏相关
- bfs 和 dfs 专题
- TCP/IP详解学习笔记--TCP的超时与重传