# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回构造的TreeNode根节点
def reConstructBinaryTree(self, pre, tin):
# write code here
if not pre or not tin:
return None
root = TreeNode(pre.pop(0))
root_index = tin.index(root.val)
root.left = self.reConstructBinaryTree(pre,tin[:root_index])
root.right = self.reConstructBinaryTree(pre,tin[root_index + 1:])
return root
剑指offer习题四
最新推荐文章于 2023-05-07 22:52:08 发布