剑指offer习题四

# -*- 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

猜你喜欢

转载自blog.csdn.net/u012693077/article/details/80785062