Leetcode-105.从前序与中序遍历序列构造二叉树

题目链接

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
        if not preorder or not inorder:
            return None
        root=TreeNode(preorder[0])
        root_index=inorder.index(preorder[0])
        root.left=self.buildTree(preorder[1:1+root_index],inorder[:root_index])
        root.right=self.buildTree(preorder[1+root_index:],inorder[root_index+1:])
        return root

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-05 22:12:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-05 22:12:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-05 22:12:03       82 阅读
  4. Python语言-面向对象

    2024-01-05 22:12:03       91 阅读

热门阅读

  1. Mysql面试题

    2024-01-05 22:12:03       47 阅读
  2. atoi函数的模拟实现

    2024-01-05 22:12:03       51 阅读
  3. 76 BFS解单词接龙

    2024-01-05 22:12:03       56 阅读
  4. c# 设置文件夹隐藏

    2024-01-05 22:12:03       59 阅读
  5. LeetCode解法汇总1276. 不浪费原料的汉堡制作方案

    2024-01-05 22:12:03       57 阅读
  6. React Grid Layout基础使用

    2024-01-05 22:12:03       51 阅读
  7. Css中默认与继承

    2024-01-05 22:12:03       60 阅读
  8. SSH协议中发现新安全漏洞CVE-2023-48795

    2024-01-05 22:12:03       74 阅读
  9. 变量和对象的解构赋值

    2024-01-05 22:12:03       56 阅读
  10. android c++打印堆栈

    2024-01-05 22:12:03       68 阅读
  11. PHP命令行脚本接收传入参数的三种方式

    2024-01-05 22:12:03       50 阅读