根据前序 中序 后序 构建二叉树
来源:互联网 发布:类似于matlab的软件 编辑:程序博客网 时间:2024/06/05 15:39
//Construct Binary Tree from Inorder and Postorder #include <iostream>#include <vector>using namespace std;struct TreeNode{ int val; TreeNode *left, *right; TreeNode(int data): val(data), left(NULL), right(NULL){ }};class Solution{ public: //根据中序和后序遍历 构建二叉树 TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder){ return buildTree(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1); } TreeNode *buildTree(vector<int> &inorder, int iLeft, int iRight, vector<int> &postorder, int pLeft, int pRight) { if(iLeft > iRight || pLeft > pRight){ return NULL; } TreeNode *cur = new TreeNode (postorder[pRight]); int i = 0; for(i = iLeft; i < inorder.size(); ++i){ if(inorder[i] == cur->val){ break; } } cur->left = buildTree(inorder, iLeft, i - 1, postorder, pLeft, pLeft + i - iLeft - 1); cur->right = buildTree(inorder, i + 1, iRight, postorder, pLeft + i - iLeft, pRight - 1); return cur; } //根据前序和中序遍历 构建二叉树 TreeNode *buildTree2(vector<int> &preorder, vector<int> &inorder){ return buildTree2(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1); } TreeNode *buildTree2(vector<int> &preorder, int iLeft, int iRight, vector<int> &inorder, int pLeft, int pRight) { if(iLeft > iRight || pLeft > pRight){ return NULL; } TreeNode *cur = new TreeNode(preorder[iLeft]); int i; //for(i = 0; i < inorder.size(); ++i){ for(i = pLeft; i <= pRight; ++i){ if(inorder[i] == cur->val){ break; } } cur->left = buildTree2(preorder, iLeft + 1, iLeft + i - pLeft , inorder, pLeft , i - 1); cur->right = buildTree2(preorder, iLeft + i - pLeft + 1, iRight, inorder, i + 1, pRight); return cur; }};//前序遍历输出void PreorderDisplay(TreeNode *root){ if(root == NULL){ return ; } else { cout << root->val <<" "; PreorderDisplay(root->left); PreorderDisplay(root->right); }}//后序遍历输出void PostorderDisplay(TreeNode *root){ if(root == NULL){ return ; } else { PostorderDisplay(root->left); PostorderDisplay(root->right); cout << root->val << " "; }}int main(){ Solution s; TreeNode *res, *res1; vector<int> inorder = {11,4,5,13,8,9}; vector<int> preorder = {5,4,11,8,13,9}; vector<int> postorder = {11,4,13,9,8,5};// res = s.buildTree(inorder, postorder);// PreorderDisplay(res);// cout << endl; cout << 2 << endl; res1 = s.buildTree2(preorder, inorder); cout << 2 << endl; //cout << res1->val << endl; PostorderDisplay(res1); system("pause"); return 0;}
阅读全文
0 0
- 根据前序 中序 后序 构建二叉树
- 根据前序和后序构建二叉树
- 根据前序遍历、中序遍历构建二叉树,并后序遍历输出。
- 二叉树系列——根据前序和中序、中序和后序构建二叉树
- 根据前序遍历,中序遍历构建二叉树
- 根据前序和中序,构建二叉树
- 根据前序、中序构建二叉树
- 根据中序和前序遍历构建二叉树
- 根据前序和中序构建二叉树
- java实现根据前序遍历构建二叉树(前序遍历、中序遍历、后序遍历)
- 根据前序遍历和中序遍历构建二叉树以及根据中序遍历后序遍历构建二叉树
- 根据树的后序和中序序列或者前序和中序序列构建二叉树,
- 根据前序与后序遍历构建二叉树,并以后序遍历结果输出
- 根据二叉树的前序和中序构建二叉树
- 根据二叉树的前序和中序获得后序,后序和中序获得前序
- 根据树的前序遍历与中序遍历构建二叉树
- 剑指offer 面试题6 (根据前序和中序 构建二叉树)
- 根据前序遍历和中序遍历构建二叉树
- 专家:手机仅需要4GB运存就够用
- 在结构体最后定义一个长度为0的字符数组(技巧)
- PHP 7 新特性
- Mac安装thrift-0.10.0
- 前端框架这么多,该如何选择?
- 根据前序 中序 后序 构建二叉树
- 安卓自定义定时器
- struct和typedef struct 总结
- 敌兵布阵
- RStudio常用快捷键
- Spire.Doc 教程:从C#和VB.NET的word文档中删除形状
- tomcat7启动报错Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]
- 设计模式之单例模式
- 28事件