力扣 105. 从前序与中序遍历序列构造二叉树 2022-08-06 leetcode Word count: 61 | Reading time≈ 1 min 前置解法 前置12345COPYclass TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right 解法1234567COPYdef buildTree(preorder, inorder): if not preorder: return root = TreeNode(preorder[0]) root_idx = inorder.index(preorder.pop(0)) root.left = self.buildTree(preorder[:root_idx], inorder[:root_idx]) root.right = self.buildTree(preorder[root_idx:], inorder[root_idx + 1:]) return root Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source. 上一篇 力扣 114. 二叉树展开为链表 下一篇 力扣 104. 二叉树的最大深度