力扣105. 从前序与中序遍历序列构造二叉树

Problem: 105. 从前序与中序遍历序列构造二叉树

文章目录

题目描述

思路

首先明确:

1.前序遍历 的顺序是根-左-右 (即先遍历根节点,再左子树,再右子树)

2.中序遍历 的顺序是左-右-根

1.前序遍历数组中的第一个元素一定是树的根节点

2.每次在前序数组中找出根节点,再到的中序数组中找出根节点,则在中序数组中该根节点的左侧为根节点的左子树、右侧是根节点的右子树(则我们每次要重复该操作去构造二叉树)

解题方法

1.定义构造二叉树的递归函数TreeNode* build(vector& preorder, int preStart, int preEnd, vector& inorder, int inStart, int inEnd)其中preStart 是每次递归前序遍历数组中的起始位置preEnd 是每次递归前序遍历数组中的结束位置,同理inStart 每次递归中序遍历数组的起始位置inEnd 每次递归数组的结束位置

2.定义一个unordered_map<int, int>valToIndex;在中序遍历数组中每次根节点的索引位置
3.每次递归中先在先序数组中取出
当前的父节点
再再中序数组中找出当前父节点的索引(index) ,通过计算得到左子树所有节点的长度:int leftSize = index - inStart;

4.下一次递归的起始与结束位置为preStart + 1, preStart + leftSize,inStart, index - 1 ,preStart + leftSize + 1,index + 1, inEnd

复杂度

时间复杂度:

O ( n ) O(n) O(n);其中 n n n为树节点的个数

空间复杂度:

O ( h e i g h ) O(heigh) O(heigh);其中 h e i g h t height height为树的高度

Code

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 {
    //Recode the element of inorder array into the unorderd_map<int, int>
    unordered_map<int, int>valToIndex;
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        for (int i = 0; i < inorder.size(); ++i) {
            valToIndex[inorder[i]] = i;
        }

        return build(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
    }

    TreeNode* build(vector<int>& preorder, int preStart, int preEnd, vector<int>& inorder, int inStart, int inEnd) {
        //Base case
        if (preStart > preEnd) {
            return nullptr;
        }
        //The first element in preorder array is the root of binary tree that
        //we shoud creat
        int rootVal = preorder[preStart];
        //Find the index of rootVal in inorder array
        int index = valToIndex[rootVal];
        //Calculate the size of left tree
        int leftSize = index - inStart;

        //Create the root firstly
        TreeNode* root = new TreeNode(rootVal);
        // Recursively construct the left and right subtrees
        root -> left = build(preorder, preStart + 1, preStart + leftSize, inorder, inStart, index - 1);
        root -> right = build(preorder, preStart + leftSize + 1, preEnd, inorder, index + 1, inEnd);
        return root;
    }
};
相关推荐
helloworldandy1 天前
高性能图像处理库
开发语言·c++·算法
2401_836563181 天前
C++中的枚举类高级用法
开发语言·c++·算法
bantinghy1 天前
Nginx基础加权轮询负载均衡算法
服务器·算法·nginx·负载均衡
chao1898441 天前
矢量拟合算法在网络参数有理式拟合中的应用
开发语言·算法
代码无bug抓狂人1 天前
动态规划(附带入门例题)
c语言·算法·动态规划
weixin_445402301 天前
C++中的命令模式变体
开发语言·c++·算法
季明洵1 天前
C语言实现顺序表
数据结构·算法·c·顺序表
南风知我意9571 天前
【前端面试2】基础面试(杂项)
前端·面试·职场和发展
Hgfdsaqwr1 天前
实时控制系统优化
开发语言·c++·算法
2301_821369611 天前
嵌入式实时C++编程
开发语言·c++·算法