4. Reconstruction binary tree (Python)

Reconstruction according to the preamble and the binary sequence:

1  class Solution:
 2      #返回构造的TreeNode根节点
 3      def reConstructBinaryTree (self, for, tin):
 4          # write code here
 5          if only (for) == 0 :
 6              return None
 7          if only (for) == 1 :
 8              return TreeNode (the [ 0 ])
 9          root TreeNode = (the [ 0 ])
 10          Tinly = t [ 0 : tin.index (the [ 0 ])]
 11  
12          tinR = t [tin.index (the [ 0])+1:]
13         root.left = self.reConstructBinaryTree(pre[1:tin.index(pre[0])+1],tinL)
14         root.right = self.reConstructBinaryTree(pre[tin.index(pre[0])+1:],tinR)
15         return root

2019-12-31 12:43:06

Guess you like

Origin www.cnblogs.com/NPC-assange/p/12123849.html