题解 | #重建二叉树#
重建二叉树
https://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6
class Solution: def reConstructBinaryTree(self , pre: List[int], vin: List[int]) -> TreeNode: # write code here if not pre: return None node = pre[0] id_vin = vin.index(node) left_vin = vin[:id_vin] l_left = len(left_vin) right_vin = vin[id_vin+1:] left_pre = pre[1: 1+l_left] right_pre = pre[1+l_left:] root = TreeNode(node) root.left = self.reConstructBinaryTree(left_pre, left_vin) root.right = self.reConstructBinaryTree(right_pre, right_vin) return root