剑指offer——面试题6:重建二叉树

来源:互联网 发布:淘宝店发货地址 编辑:程序博客网 时间:2024/06/05 07:01

    题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{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:    struct TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {        //判定递归终止条件;如果不加会导致递归无休止的循环下去,会显示段错误之类的        if(pre.size() == 0 || vin.size() == 0) {            return NULL;        }        //定义Node节点并其求根节点;        int root = pre[0];    // 第一次是 1        TreeNode* node = new TreeNode(root);        vector<int>::iterator it;        //1.求左右子树的遍历序列;        vector<int> preLeft, preRight, vinLeft, vinRight;            //(1).求根节点在中序遍历序列中的位置;        vector<int>::iterator i;        for(it = vin.begin(); it != vin.end(); it++) {            if(root == *it) {                i = it;            }        }            //(2).求左右子树的中序遍历子序列;        int k = 0;        for(it = vin.begin(); it != vin.end(); it++) {            if(k == 0) {                vinLeft.push_back(*it);            }            else if(k == 1) {                vinRight.push_back(*it);            }            else {}            if(it == i) {                k = 1;            }         }            //(3).求左右子树的前序遍历子序列;        k = 0;        vector<int>::iterator ite;        for(it = pre.begin()+1; it != pre.end(); it++) {            for(ite = vinLeft.begin(); ite != vinLeft.end(); ite++) {                if(*it == *ite) {                    preLeft.push_back(*it);                    k = 1;                }            }            if(k == 0) {                preRight.push_back(*it);            }            k = 0;        }        //根据遍历序列求出跟的左右节点;        node->left = reConstructBinaryTree(preLeft,vinLeft);        node->right = reConstructBinaryTree(preRight,vinRight);        //返回节点地址;        return node;    }};

    分析:在二叉树的前序遍历序列中,第一个数字总是树的根结点的值。但在中序遍历序列中,根结点的值在序列的中间,左子树的结点的值位于根结点的值的左边,而右子树的结点的值位于根结点的值的右边。因此我们需要扫描中序遍历序列,才能找到根结点的值。

    如下图所示,前序遍历序列的第一个数字1就是根结点的值。扫描中序遍历序列,就能确定根结点的值的位置。根据中序遍历特点,在根结点的值1前面的3个数字都是左子树结点的值,位于1后面的数字都是右子树结点的值。

    在二叉树的前序遍历和中序遍历的序列中确定根结点的值、左子树结点的值和右子树结点的值既然我们已经分别找到了左、右子树的前序遍历序列和中序遍历序列,我们可以用同样的方法分别去构建左右子树。也就是说,接下来的事情可以用递归的方法去完成。

    比如说:

        

    

    《剑指offer》上的代码:

    先说一下,《剑指offer》上给的函数的形参,和牛客网不同,因此程序有较大差别,,不过,感觉还是剑指offer上写的更简洁,更鲁棒。

BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder);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){    // 前序遍历序列的第一个数字是根结点的值    int rootValue = startPreorder[0];    BinaryTreeNode* root = new BinaryTreeNode();    root->m_nValue = rootValue;    root->m_pLeft = root->m_pRight = NULL;    if(startPreorder == endPreorder)    {        if(startInorder == endInorder && *startPreorder == *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 = 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;}// ====================测试代码====================void Test(char* testName, int* preorder, int* inorder, int length){    if(testName != NULL)        printf("%s begins:\n", testName);    printf("The preorder sequence is: ");    for(int i = 0; i < length; ++ i)        printf("%d ", preorder[i]);    printf("\n");    printf("The inorder sequence is: ");    for(int i = 0; i < length; ++ i)        printf("%d ", inorder[i]);    printf("\n");    try    {        BinaryTreeNode* root = Construct(preorder, inorder, length);        PrintTree(root);        DestroyTree(root);    }    catch(std::exception& exception)    {        printf("Invalid Input.\n");    }}// 普通二叉树//              1//           /     \//          2       3  //         /       / \//        4       5   6//         \         ///          7       8void Test1(){    const int length = 8;    int preorder[length] = {1, 2, 4, 7, 3, 5, 6, 8};    int inorder[length] = {4, 7, 2, 1, 5, 3, 8, 6};    Test("Test1", preorder, inorder, length);}// 所有结点都没有右子结点//            1//           / //          2   //         / //        3 //       ///      4//     ///    5void Test2(){    const int length = 5;    int preorder[length] = {1, 2, 3, 4, 5};    int inorder[length] = {5, 4, 3, 2, 1};    Test("Test2", preorder, inorder, length);}// 所有结点都没有左子结点//            1//             \ //              2   //               \ //                3 //                 \//                  4//                   \//                    5void Test3(){    const int length = 5;    int preorder[length] = {1, 2, 3, 4, 5};    int inorder[length] = {1, 2, 3, 4, 5};    Test("Test3", preorder, inorder, length);}// 树中只有一个结点void Test4(){    const int length = 1;    int preorder[length] = {1};    int inorder[length] = {1};    Test("Test4", preorder, inorder, length);}// 完全二叉树//              1//           /     \//          2       3  //         / \     / \//        4   5   6   7void Test5(){    const int length = 7;    int preorder[length] = {1, 2, 4, 5, 3, 6, 7};    int inorder[length] = {4, 2, 5, 1, 6, 3, 7};    Test("Test5", preorder, inorder, length);}// 输入空指针void Test6(){    Test("Test6", NULL, NULL, 0);}// 输入的两个序列不匹配void Test7(){    const int length = 7;    int preorder[length] = {1, 2, 4, 5, 3, 6, 7};    int inorder[length] = {4, 2, 8, 1, 6, 3, 7};    Test("Test7: for unmatched input", preorder, inorder, length);}int _tmain(int argc, _TCHAR* argv[]){    Test1();    Test2();    Test3();    Test4();    Test5();    Test6();    Test7();    return 0;}