重建二叉树

来源:互联网 发布:君の知らない物语 编辑:程序博客网 时间:2024/05/29 10:26

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

方法一:

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {//先通过前序遍历找到跟节点        //再通过跟节点把中序遍历的结果分为左子树tree_left和右子树tree_right        //tree_left在前序遍历中的第一个节点就是跟节点的左孩子        //tree_right在前序遍历中的第一个节点就是跟节点的右孩子        if (pre.size() == 0) return NULL;        /*if (pre.empty() || in.empty() || pre.size() != in.size())            return NULL;*/        TreeNode* root = new TreeNode(pre[0]); //前序遍历的第一个节点就是跟节点    if (pre.size() == 1) return root;        vector<int> in_left, in_right;//中序遍历中的左子树和右子树        vector<int> pre_left, pre_right;//前序遍历中的左子树和右子树        int num_left=0, num_right=0; //分别存储左子树和右子树的个数        int cur = pre[0]; //存储谦虚遍历中的第一个值,根据这个值在将中序遍历分为左子树和右子树            int pos = 0;//中序遍历中cur中的位置                for (int i = 0;i < vin.size();i++)  //计算中序遍历的左子树和右子树        {            if (vin[i] == cur)                pos = i;        }        for (int i = 0;i < vin.size();i++)        {            if (i < pos)            {                in_left.push_back(vin[i]);                num_left++;            }                            if (i > pos)            {                in_right.push_back(vin[i]);                num_right++;            }        }        for (int i = 1;i < pre.size();i++) //计算先序遍历的左子树和右子树        {            if (num_left)            {                pre_left.push_back(pre[i]);                --num_left;            }            else if (num_right)            {                pre_right.push_back(pre[i]);                --num_right;            }            else            {            }        }        //if(!pre_left.empty()&&root!=NULL)            root->left=reConstructBinaryTree(pre_left,in_left);        //if(!pre_right.empty() && root != NULL)            root->right = reConstructBinaryTree(pre_right,in_right);            return root;//最后返回根节点,这点很重要,之前都是因为这个一直调试不通过    }};

方法二:

#include<stdio.h>#include <iostream>using namespace std;struct BinaryTreeNode{int m_nValue;BinaryTreeNode *m_pLeft;BinaryTreeNode *m_pRight;};BinaryTreeNode* ConstructCore(int *startPreorder,int *endPreorder,int *startInorder,int *endInoder);BinaryTreeNode * Construct(int *preorder, int *inorder, int length){if(preorder == NULL || inorder == NULL || length <= 0){return NULL;}return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);}BinaryTreeNode* ConstructCore(int *startPreorder,int *endPreorder,int *startInorder,int *endInorder){BinaryTreeNode* root = new BinaryTreeNode();int rootValue = startPreorder[0];root->m_nValue = rootValue;root->m_pLeft = NULL;root->m_pRight = NULL;if(startPreorder == endPreorder){if(startInorder == endInorder && startPreorder == startInorder){return root;}else{//throw std::exception("Invalid input!\n");}}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 = startPreorder + leftLength;if(leftLength > 0){root->m_pLeft = ConstructCore(startPreorder + 1, leftPreorderEnd, startInorder, rootInorder - 1);}if(leftLength < endPreorder - startPreorder){root->m_pRight = ConstructCore(leftPreorderEnd + 1, endPreorder, rootInorder + 1, endInorder);}return root;}int main(){return 0;}

原创粉丝点击