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;
    }
};
相关推荐
汉汉汉汉汉8 分钟前
C++11新特性详解:从列表初始化到线程库
c++
楼田莉子1 小时前
C++算法题目分享:二叉搜索树相关的习题
数据结构·c++·学习·算法·leetcode·面试
小明的小名叫小明2 小时前
区块链技术原理(14)-以太坊数据结构
数据结构·区块链
pusue_the_sun2 小时前
数据结构——栈和队列oj练习
c语言·数据结构·算法··队列
大锦终2 小时前
【算法】模拟专题
c++·算法
方传旺2 小时前
C++17 std::optional 深拷贝 vs 引用:unordered_map 查询大对象性能对比
c++
Dontla3 小时前
Makefile介绍(Makefile教程)(C/C++编译构建、自动化构建工具)
c语言·c++·自动化
Xの哲學3 小时前
Perf使用详解
linux·网络·网络协议·算法·架构
奶黄小甜包3 小时前
C语言零基础第18讲:自定义类型—结构体
c语言·数据结构·笔记·学习
想不明白的过度思考者3 小时前
数据结构(排序篇)——七大排序算法奇幻之旅:从扑克牌到百亿数据的魔法整理术
数据结构·算法·排序算法