LeetCode106 从中序和后序序列构造二叉树

题目描述:

根据一棵树的中序遍历与后序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]

返回如下的二叉树:

    3
   / 
  9  20
    /  
   15   7






/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
/*
算法思想:
    由于后序的顺序的最后一个肯定是根,所以原二叉树的根节点可以知道,题目中给了一个很关键的条件就是树中没有相同元素,有了这个条件我们就可以在中序遍历中也定位出根节点的位置,并以根节点的位置将中序遍历拆分为左右两个部分,分别对其递归调用原函数。
    需要小心的地方就是递归时postorder的左右index很容易写错,比如 pLeft + i - iLeft - 1, 这个又长又不好记,首先我们要记住 i - iLeft 是计算inorder中根节点位置和左边起始点的距离,然后再加上postorder左边起始点然后再减1。我们可以这样分析,如果根节点就是左边起始点的话,那么拆分的话左边序列应该为空集,此时i - iLeft 为0, pLeft + 0 - 1 < pLeft, 那么再递归调用时就会返回NULL, 成立。如果根节点是左边起始点紧跟的一个,那么i - iLeft 为1, pLeft + 1 - 1 = pLeft,再递归调用时还会生成一个节点,就是pLeft位置上的节点,为原二叉树的一个叶节点。
*/
//算法实现:

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;
    }
};

/*
算法思想:
    与中序遍历和前序遍历构造二叉树的过程类似。只不过对于后序遍历来说,根节点是最后一个被访问的节点。
*/
//算法实现:

class Solution {
public:
    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { //以向量形式给出中序和后序序列
        if(postorder.size()==0||inorder.size()==0){ //序列有一个为空,构建的树为空
            return NULL;
        }
        if(postorder.size()!=inorder.size()){   ////序列长度不相同,构建的树为空
            return NULL;
        }
         
        vector<int> inorder_l,inorder_r,postorder_l,postorder_r;    ////辅助空间,存放被分割开的中序和后序遍历的序列
        int root_index=-1,len = postorder.size();
        TreeNode* root=new TreeNode(postorder[len-1]);  //根节点即后序尾结点
         
        for(int i=0;i<len;i++){ //在中序队列中找出根节点位置
            if(postorder[len-1]==inorder[i]){
                root_index=i;
                break;
            }
        }
         
        for(int i=0; i<root_index; i++) {   //  左右子树的后序、中序序列
            postorder_l.push_back(postorder[i]);
            inorder_l.push_back(inorder[i]);
        }
        for(int i=root_index+1; i<inorder.size(); i++) {
            postorder_r.push_back(postorder[i-1]);  //这里要注意
            inorder_r.push_back(inorder[i]);
        }
        root->left=buildTree(inorder_l, postorder_l);   //递归重建左子树
        root->right=buildTree(inorder_r, postorder_r);  //递归重建右子树
        return root;  
    }
};
原文地址:https://www.cnblogs.com/parzulpan/p/9939936.html