【剑指Offer】面试题07. 重建二叉树

题目

输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。

例如,给出

前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]

返回如下的二叉树:

    3
   / \
  9  20
    /  \
   15   7

限制:
0 <= 节点个数 <= 5000

思路:递归

【LeetCode】105. 从前序与中序遍历序列构造二叉树
关键在与正确定位左右子树范围。

代码

class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if (preorder.empty()) return nullptr;
        return helper(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
    }

    TreeNode* helper(vector<int> &preorder, int pstart, int pend, vector<int> &inorder, int istart, int iend) {
        if (pend < pstart) return nullptr;
        int val = preorder[pstart];
        TreeNode* root = new TreeNode(val);
        auto it = find(inorder.begin() + istart, inorder.begin() + (iend + 1), val);//注意在[istart, iend]范围内搜索
        int lenLeft = it - find(inorder.begin() + istart, inorder.begin() + (iend + 1), inorder[istart]);
        root->left = helper(preorder, pstart + 1, pstart + lenLeft, inorder, istart, istart + lenLeft - 1);
        root->right = helper(preorder, pstart + lenLeft + 1, pend, inorder, istart + lenLeft + 1, iend);
        return root;
    }
};

另一种写法

修改求根节点索引。

class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if (preorder.empty()) return nullptr;
        return helper(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
    }

    TreeNode* helper(vector<int> &preorder, int pstart, int pend, vector<int> &inorder, int istart, int iend) {
        if (pend < pstart) return nullptr;
        int val = preorder[pstart];
        TreeNode* root = new TreeNode(val);
        auto it = find(inorder.begin() + istart, inorder.begin() + (iend + 1), val);
        int index = it - inorder.begin();        
        int lenLeft = index - istart;
        root->left = helper(preorder, pstart + 1, pstart + lenLeft, inorder, istart, index - 1);
        root->right = helper(preorder, pstart + lenLeft + 1, pend, inorder, index + 1, iend);
        return root;
    }
};
posted @ 2020-02-13 19:51  Galaxy_hao  阅读(325)  评论(0编辑  收藏  举报