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 <= inorder[i], postorder[i] <= 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;
    }
};
相关推荐
六点半8888 分钟前
【C++】C++11 篇二
开发语言·c++
要开心吖ZSH11 分钟前
软件设计师备考-(十六)数据结构及算法应用(重要)
java·数据结构·算法·软考·软件设计师
DDDDDDDRDDR16 分钟前
C++容器:list
开发语言·c++·stl
一拳一个呆瓜18 分钟前
【MFC】对话框属性:Use System Font(使用系统字体)
c++·mfc
Elnaij21 分钟前
从C++开始的编程生活(7)——取地址运算符重载、类型转换、static成员和友元
开发语言·c++
带娃的IT创业者24 分钟前
如何开发一个教育性质的多线程密码猜测演示器
网络·python·算法
郝学胜-神的一滴39 分钟前
Effective Modern C++ 条款26:避免在通用引用上重载
开发语言·c++·程序人生
草莓熊Lotso42 分钟前
【C++】递归与迭代:两种编程范式的对比与实践
c语言·开发语言·c++·经验分享·笔记·其他
zhong liu bin2 小时前
MySQL数据库面试题整理
数据结构·数据库·mysql
Aczone282 小时前
硬件(六)arm指令
开发语言·汇编·arm开发·嵌入式硬件·算法