深度优先搜索DFS:力扣105从前序与中序遍历序列构造二叉树&力扣106从中序与后序遍历序列构造二叉树

一、从前序与中序遍历序列构造二叉树

1、题目描述

在这里插入图片描述
2、题解:
此题考查二叉树,并且可用深度优先搜索去做题。找到规律,利用递归去做题。我们可以自己画一个二叉树,然后写出前序和中序遍历,然后根据两者重新画一颗二叉树。递归结束的条件是两个数组有一个为空,并且要判断如果长度不相等直接返回None。前序遍历就是“根左右”也就是第一个数一定是根,中序遍历是“左根右”。如下图:先以preorder[0]创建根,然后确定根节点在inorder中的位置index,然后寻找左右子树,递归就可以了,左子树:preorder[1:index+1](注意切片是左闭右开的),inorder[:index];右子树:preorder[index+1:],inorder[index+1:]
在这里插入图片描述

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
        if not preorder or not inorder:#递归结束的条件
            return None
        if len(preorder) != len(inorder):#不存在
            return None
        root = TreeNode(preorder[0])#创建根节点
        index = inorder.index(preorder[0])#找到根节点在中序遍历的位置
        root.left = self.buildTree(preorder[1:index+1],inorder[:index])#构建左子树
        root.right = self.buildTree(preorder[index+1:],inorder[index+1:])#构建右子树
        return root

或者:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
        if not preorder or not inorder:
            return None
        if len(preorder) != len(inorder):
            return None
        root = TreeNode(preorder[0])        
        for i in range(len(inorder)):#寻找根节点在中序遍历的位置
            if inorder[i] == preorder[0]:
                index = i
                break
        root.left = self.buildTree(preorder[1:index+1],inorder[:index])
        root.right = self.buildTree(preorder[index+1:],inorder[index+1:])
        return root

3、复杂度分析:
时间复杂度:O(N)
空间复杂度:O(N)

二、从中序与后序遍历序列构造二叉树

1、题目描述:
在这里插入图片描述
2、题解:

此题考查二叉树,并且可用深度优先搜索去做题。找到规律,利用递归去做题。我们可以自己画一个二叉树,然后写出中序和后序遍历,然后根据两者重新画一颗二叉树。递归结束的条件是两个数组有一个为空,并且要判断如果长度不相等直接返回None。后序遍历就是“左右根”也就是第一个数一定是根,中序遍历是“左根右”。如下图:先以postorder[-1]创建根,然后确定根节点在inorder中的位置index,然后寻找左右子树,递归就可以了,左子树:inorder[:index],postorder[:index](注意切片是左闭右开的);右子树:inorder[index+1:],postorder[index:-1].
在这里插入图片描述

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
        if not inorder or not postorder: #递归结束的条件
            return None
        if len(inorder) != len(postorder):
            return None
        root = TreeNode(postorder[-1])#创建根节点
        index = inorder.index(postorder[-1])#找到根节点在中序遍历的位置
        root.left = self.buildTree(inorder[:index],postorder[:index])#构建左子树
        root.right = self.buildTree(inorder[index+1:],postorder[index:-1])#构建右子树
        return root

C++:
递归:中序遍历、后序遍历模板

// 中序遍历
void inorder(TreeNode* root) {
    if (root == nullptr) {
        return;
    }
    inorder(root->left);
    ans.push_back(root->val);
    inorder(root->right);
}
// 后序遍历
void postorder(TreeNode* root) {
    if (root == nullptr) {
        return;
    }
    postorder(root->left);
    postorder(root->right);
    ans.push_back(root->val);
}
用C++实现:

4、复杂度分析:

时间复杂度都为O(N)
空间复杂度都为O(N)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值