力扣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;
    }
};
相关推荐
fai厅的秃头姐!2 小时前
C语言03
c语言·数据结构·算法
lisanndesu2 小时前
动态规划
算法·动态规划
myprogramc2 小时前
十大排序算法
数据结构·算法·排序算法
记得早睡~2 小时前
leetcode150-逆波兰表达式求值
javascript·算法·leetcode
修己xj2 小时前
算法系列之贪心算法
算法
qy发大财2 小时前
跳跃游戏(力扣55)
算法·leetcode
BingLin-Liu2 小时前
蓝桥杯备考:搜索算法之排列问题
算法·职场和发展·蓝桥杯
计算机小白一个2 小时前
蓝桥杯 Java B 组之岛屿数量、二叉树路径和(区分DFS与回溯)
java·数据结构·算法·蓝桥杯
curemoon3 小时前
理解都远正态分布中指数项的精度矩阵(协方差逆矩阵)
人工智能·算法·矩阵
柃歌3 小时前
【UCB CS 61B SP24】Lecture 7 - Lists 4: Arrays and Lists学习笔记
java·数据结构·笔记·学习·算法