LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal

文章目录

一、题目

Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.

Example 1:

Input: inorder = 9,3,15,20,7, postorder = 9,15,7,20,3

Output: 3,9,20,null,null,15,7

Example 2:

Input: inorder = -1, postorder = -1

Output: -1

Constraints:

1 <= inorder.length <= 3000

postorder.length == inorder.length

-3000 <= inorderi, postorderi <= 3000

inorder and postorder consist of unique values.

Each value of postorder also appears in inorder.

inorder is guaranteed to be the inorder traversal of the tree.

postorder is guaranteed to be the postorder traversal of the tree.

二、题解

注意切分完中序数组后,需要用postorder.resize(postorder.size() - 1);对后序数组进行修改

cpp 复制代码
/**
 * 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 {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(postorder.size() == 0) return nullptr;
        int rootVal = postorder[postorder.size()-1];
        TreeNode* root = new TreeNode(rootVal);
        if(postorder.size() == 1) return root;
        //切中序数组
        int index = 0;
        for(int i = 0;i < inorder.size();i++){
            if(inorder[i] == rootVal){
                index = i;
                break;
            }
        }
        vector<int> leInorder;
        vector<int> riInorder;
        for(int i = 0;i < index;i++){
            leInorder.push_back(inorder[i]);
        }
        for(int i = index + 1;i < inorder.size();i++){
            riInorder.push_back(inorder[i]);
        }
        postorder.resize(postorder.size() - 1);
        //切后序数组
        vector<int> lePostorder;
        vector<int> riPostorder;
        for(int i = 0;i < leInorder.size();i++){
            lePostorder.push_back(postorder[i]);
        }
        for(int i = leInorder.size();i < postorder.size();i++){
            riPostorder.push_back(postorder[i]);
        }
        root->left = buildTree(leInorder,lePostorder);
        root->right = buildTree(riInorder,riPostorder);
        return root;
    }
};
相关推荐
用户813267933251 分钟前
Python 计算盘中 VWAP:为什么 1 分钟 K 线是量化工程中的高性价比选择
后端·算法·github
神仙别闹3 分钟前
基于C++ 实现 L 系统分形树
开发语言·c++
高频因子挖掘机5 分钟前
Python批量获取1000只ETF 5分钟K线:从分页到吞吐量优化的QuantDash实践
后端·算法·github
额额额对了22 分钟前
Linux C 多线程编程基础:从内存模型到生命周期管理
linux·开发语言·算法
INGNIGHT1 小时前
1584.连接所有点的最小费用(最小生成树&并查集union find)
c++·leetcode
wabs6661 小时前
关于二叉树【力扣144.二叉树的前序遍历的思考】
数据结构·c++·算法·leetcode·二叉树
郝学胜-神的一滴1 小时前
C++20 高级编程 004:从初始化、内存到const系列关键字
开发语言·算法·编程·软件构建·c++20
kyle~1 小时前
点云配准--- 迭代最近点 ICP 求解
线性代数·算法·机器学习
zmzb01031 小时前
C++课后习题训练记录Day199
开发语言·c++
LuminousCPP1 小时前
数据结构-二叉树(三):堆复杂度证明与 Top-K 问题|错位相减推导 + 海量数据内存优化
c语言·数据结构·笔记·排序算法