重建二叉树 python
作者:互联网
# -*- 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_local = tin.index(root.val)
root.left = self.reConstructBinaryTree(pre[:root_local+1], tin[:root_local])
root.right = self.reConstructBinaryTree(pre[root_local:], tin[root_local+1:])
return root
标签:pre,reConstructBinaryTree,python,local,self,tin,二叉树,root,重建 来源: https://blog.csdn.net/WANGZHUCHEN/article/details/100108220