重建二叉树
来源:互联网 发布:javascript 或运算 编辑:程序博客网 时间:2024/06/03 19:29
题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树,假设输入的前序遍历和中序遍历的结果中都不含重复的数字,例如输入前序遍历{ 1,2, 4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}则重建出图2.6所示的二叉树并输出它的头结点。
#pragma once#include<queue>struct BTNode{ int value; BTNode* left; BTNode* right;};BTNode* ConstructCore(int * startPrevorder, int* endPreorder , int* startInorder , int* endInorder){ int rootValue = startPrevorder [0]; BTNode *root = new BTNode(); root->value = rootValue; root->left = root->right = NULL; if (startPrevorder == endPreorder) { if (startInorder == endInorder &&* startPrevorder == *startInorder ) return root; else throw std::exception ("Invalid input."); } int* rootInorder = startInorder ; while (rootInorder <= endInorder &&*rootInorder != rootValue) ++rootInorder; if (rootInorder == endInorder &&*rootInorder != rootValue) throw std::exception ("Invalid input."); int leftLength = rootInorder - startInorder ; int* leftPreorderEnd = startPrevorder + leftLength; if (leftLength > 0) { root->left = ConstructCore( startPrevorder + 1, leftPreorderEnd, startInorder, rootInorder - 1); } if (leftLength < endPreorder - startPrevorder) { root->right = ConstructCore(leftPreorderEnd + 1, endPreorder, rootInorder + 1, endInorder ); } return root;}BTNode* Construct(int * prevorder, int* inorder , int length){ if (prevorder == NULL || inorder == NULL || length <= 0) return NULL ; return ConstructCore(prevorder , prevorder + length - 1, inorder, inorder + length - 1);}void Test(){ int pre[8] = { 1, 2, 4, 7, 3, 5, 6, 8 }; int in[8] = { 4, 7, 2, 1, 5, 3, 8, 6 }; BTNode* root = Construct(pre, in, 8); //PrintTree(root);}
#include<iostream>using namespace std;#include<vector>#include <algorithm> struct TreeNode{int _value;TreeNode* _left;TreeNode* _right;TreeNode(const int x):_value(x), _left(NULL), _right(NULL){}};TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> in) {TreeNode* tree = new TreeNode(pre[0]);//tree->val = pre.front();vector<int>::iterator lt = find(in.begin(), in.end(), tree->_value);//lt指向1//中序中it+1指向vector<int>::iterator rt = find_first_of(pre.begin(), pre.end(), lt + 1, in.end());//rt指向3vector<int>l1(pre.begin() + 1, rt);//2 4 7 迭代器左闭右开,所以没有3vector<int>r1(in.begin(), lt);//4 7 2 vector<int>l2(rt, pre.end()); //3 5 6 8vector<int>r2(lt + 1, in.end());//5 3 8 6if (l1.empty()) tree->_left = NULL;else//继续进行递归tree->_left = reConstructBinaryTree(l1, r1);//l1和r1全为左树的if (l2.empty())tree->_right = NULL;else //继续进行递归tree->_right = reConstructBinaryTree(l2, r2);//l2和r2全为右树情况return tree;}int main(){int prev[] = { 1, 2, 4, 7, 3, 5, 6, 8 };vector<int> pre(prev, prev + sizeof(prev) / sizeof(int));int ins[] = { 4, 7, 2, 1, 5, 3, 8, 6 };vector<int> in(ins, ins + sizeof(ins) / sizeof(int));TreeNode* root = reConstructBinaryTree( pre, in);system("pause");return 0;}
1 0
- 3.9重建二叉树
- 重建二叉树
- 二叉树重建
- 二叉树重建
- 二叉树的重建
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 二叉树重建
- 二叉树重建
- 重建二叉树
- 重建二叉树
- 重建二叉树
- [从头学声学] 第214节 声音的升频
- 优化Privoxy的打开和关闭
- 01.C++ STL简介
- android开发之synchronized的用法
- iOS中如何在欢迎界面中动态加载广告
- 重建二叉树
- servlet写的文件上传
- rtmp协议相关知识
- 启动Hbase,HMaster进程可以启动,但是HRegionServer启动不成功
- win 10 修改资源管理器默认打开为我的电脑
- alsa-utils工具的使用
- MyBatis结果映射与MyBatis缓存初探学习记录
- [MySQL]经常无故崩溃amd.dll , boot1.exe病毒
- 使用 itext、flying-saucer 实现html转PDF