力扣刷题(day0038)从中序与后序遍历序列构造二叉树

给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

示例 1:

 输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
 输出:[3,9,20,null,null,15,7]

示例 2:

 输入:inorder = [-1], postorder = [-1]
 输出:[-1]

提示:

  • 1 <= inorder.length <= 3000

  • postorder.length == inorder.length

  • -3000 <= inorder[i], postorder[i] <= 3000

  • inorder 和 postorder 都由 不同 的值组成

  • postorder 中每一个值都在 inorder 中

  • inorder 保证是树的中序遍历

  • postorder 保证是树的后序遍历

思路:

第一步,如果数组长度为0,则说明是空节点
第二步,如果数组不为空,那么就将后序数组最后一个元素作为节点元素
第三步,找到后序数组最后一个元素在中序数组中的位置并将其作为切割点
第四步,切割中序数组,切成中序左数组和中序右数组
第五步,切割后序数组,切成后序左数组和后序右数组
第六步,递归处理左区间和右区间

便于理解的递归方法(费空间):

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    TreeNode* traversal(vector<int>& inorder,vector<int>& postorder){
        //第一步,如果数组长度为0,则为空节点
        if(postorder.size()==0) return NULL;
        //第二步,如果后序数组不为空,那么数组最后一个节点将作为节点元素
        int rootValue=postorder[postorder.size()-1];
        TreeNode* root=new TreeNode(rootValue);
        //叶子节点
        if(postorder.size()==1)return root;
        //第三步,找到后序数组中的最后的一个元素在中序数组中的位置并将其作为切割点
        int delimiterIndex;
        for(delimiterIndex=0;delimiterIndex<inorder.size();delimiterIndex++){
            if(inorder[delimiterIndex]==rootValue)break;
        }
        //第四步,切割中序数组,分为左数组和右数组
        //左闭右开区间,[0,delimiterIndex)
        vector<int>leftInorder(inorder.begin(),inorder.begin()+delimiterIndex);
        //[delimiterIndex+1,end)
        vector<int>rightInorder(inorder.begin()+delimiterIndex+1,inorder.end());

        //第五步,切割后序数组,分为左数组和右数组
        //舍弃postorder末尾元素
        postorder.resize(postorder.size()-1);
        //依然是左闭右开,这里使用了左中序的长度作为切割点
        //[0,leftInorder.size())
        vector<int>leftPostorder(postorder.begin(),postorder.begin()+leftInorder.size());
        //[leftInorder.size(),end)
        vector<int>rightPostorder(postorder.begin()+leftInorder.size(),postorder.end());

        //第六步,递归处理左右区间
        root->left=traversal(leftInorder,leftPostorder);
        root->right=traversal(rightInorder,rightPostorder);

        return root;

    }
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(!inorder.size()||!inorder.size())return nullptr;
        return traversal(inorder,postorder);
    }
};

利用下标分割数组的方法:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    TreeNode* traversal(vector<int>& inorder,int inorderBegin,int inorderEnd,vector<int>& postorder,int postorderBegin,int postorderEnd){
        //第一步,判断后序数组是否为空
        if(postorderEnd==postorderBegin)return NULL;
        //第二步,在后续数组中找到末尾数,构造树
        int rootValues=postorder[postorderEnd-1];
        TreeNode* root=new TreeNode(rootValues);
        //叶子节点
        if(postorderEnd-postorderBegin==1)return root;
        //第三步,在中序数组中找到切割点
        int delimiterIndex;
        for(delimiterIndex=inorderBegin;delimiterIndex<inorderEnd;delimiterIndex++){
            if(inorder[delimiterIndex]==rootValues)break;
        }
        //第四步,在中序数组中分左右组
        //左组(左闭右开)
        int leftInorderBegin=inorderBegin;
        int leftInorderEnd=delimiterIndex;
        //右组
        int rightInorderBegin=delimiterIndex+1;
        int rightInorderEnd=inorderEnd;

        //第五步,在后序数组中分左右组
        //左组
        int leftPostorderBegin=postorderBegin;
        int leftPostorderEnd=postorderBegin+delimiterIndex-inorderBegin;
        //右组
        int rightPostorderBegin=postorderBegin+delimiterIndex-inorderBegin;
        int rightPostorderEnd=postorderEnd-1;

        //第六步,递归遍历
        root->left=traversal(inorder,leftInorderBegin,leftInorderEnd,postorder,leftPostorderBegin,leftPostorderEnd);
        root->right=traversal(inorder,rightInorderBegin,rightInorderEnd,postorder,rightPostorderBegin,rightPostorderEnd);
        return root;
    }
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(inorder.size()==NULL||inorder.size()==NULL)return NULL;
        return traversal(inorder,0,inorder.size(),postorder,0,postorder.size());
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于力扣刷C++常用操作,我可以给你一些常见的操作和技巧: 1. 使用 STL 容器和算法库:STL(Standard Template Library)是 C++ 标准库中的一个重要组成部分,包含了许多常用的容器和算法。在力扣刷中,使用 STL 可以大大提高代码的效率和可读性。例如,vector 可以用来存储动态数组,sort 可以用来排等等。 2. 使用 auto 关键字:auto 关键字可以自动推导变量类型,可以减少代码量和提高可读性。例如,auto x = 1; 可以自动推导出 x 的类型为 int。 3. 使用 lambda 表达式:lambda 表达式是 C++11 中引入的一种匿名函数,可以方便地定义一些简单的函数对象。在力扣刷中,使用 lambda 表达式可以简化代码,例如在 sort 函数中自定义比较函数。 4. 使用位运算:位运算是一种高效的运算方式,在力扣刷中经常会用到。例如,左移运算符 << 可以用来计算 2 的幂次方,右移运算符 >> 可以用来除以 2 等等。 5. 使用递归:递归是一种常见的算法思想,在力扣刷中也经常会用到。例如,二叉树的遍、链表的反转等等。 6. 使用 STL 中的 priority_queue:priority_queue 是 STL 中的一个容器,可以用来实现堆。在力扣刷中,使用 priority_queue 可以方便地实现一些需要维护最大值或最小值的算法。 7. 使用 STL 中的 unordered_map:unordered_map 是 STL 中的一个容器,可以用来实现哈希表。在力扣刷中,使用 unordered_map 可以方便地实现一些需要快速查找和插入的算法。 8. 使用 STL 中的 string:string 是 STL 中的一个容器,可以用来存储字符串。在力扣刷中,使用 string 可以方便地处理字符串相关的问。 9. 注意边界条件:在力扣刷中,边界条件往往是解决问的关键。需要仔细分析目,考虑各种边界情况,避免出现错误。 10. 注意时间复杂度:在力扣刷中,时间复杂度往往是评判代码优劣的重要指标。需要仔细分析算法的时间复杂度,并尽可能优化代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值