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;
    }
};
相关推荐
八月的雨季 最後的冰吻2 分钟前
SIP协议栈--osip源码梳理
linux·服务器·网络·c++·网络协议
fancy16616641 分钟前
搜索二维矩阵 II
c++·算法·矩阵
freyazzr43 分钟前
Leetcode刷题 | Day63_图论08_拓扑排序
数据结构·c++·算法·leetcode·图论
暴龙胡乱写博客1 小时前
机器学习 --- KNN算法
人工智能·算法·机器学习
一梦浮华1 小时前
自学嵌入式 day 18 - 数据结构 1
数据结构
顾子茵1 小时前
c++从入门到精通(四)--动态内存,模板与泛型编程
java·开发语言·c++
ai.Neo1 小时前
牛客网NC22015:最大值和最小值
数据结构·c++·算法
Swift社区2 小时前
LeetCode 高频题实战:如何优雅地序列化和反序列化字符串数组?
算法·leetcode·职场和发展
醍醐三叶3 小时前
C++核心编程--3 函数提高
c++
徐子童3 小时前
《从零开始入门递归算法:搜索与回溯的核心思想 + 剑指Offer+leetcode高频面试题实战(含可视化图解)》
算法