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;
    }
};
相关推荐
GuWenyue3 小时前
传统Agent工具两大痛点!300行代码落地MCP跨语言工具,彻底解耦LLM与工具
前端·人工智能·算法
同勉共进4 小时前
记一例 vibe coding + gcc bug 导致的线程池死锁问题
c++·线程池·gcc·死锁·vibe coding
我叫洋洋4 小时前
C ++ [ hello world ]
c语言·c++·算法
奋发向前wcx5 小时前
y1,y2总复习笔记5 2026.7.19
数据结构·笔记·算法
王维同学8 小时前
[自学][Windows C++]RunOnceEx 注册表分组键的安全遍历
c++·windows·安全·开源
战族狼魂9 小时前
高频面试题精选:分治与AI Agent架构
人工智能·算法·大模型·大语言模型
李剑一9 小时前
你用过网易的将军令嘛?它底层实现账号保护的原理相当简单粗暴
算法
Scott9999HH9 小时前
告别流量波动玄学!从法拉第电磁定律到底层 C/C++ 流量累积算法,深度解密工业级电磁流量计选型与开发
c语言·c++·算法
中达瑞和-高光谱·多光谱9 小时前
1nm到8nm光谱分辨率,你的应用该选哪款光谱相机?
算法·高光谱·高光谱相机
脱胎换骨-军哥9 小时前
C++/Rust无缝互操作:混合系统新常态
开发语言·c++·rust