zoukankan      html  css  js  c++  java
  • 4.重建二叉树(python)

    根据前序和中序重建二叉树:

     1 class Solution:
     2     # 返回构造的TreeNode根节点
     3     def reConstructBinaryTree(self, pre, tin):
     4         # write code here
     5         if len(pre)==0:
     6             return None
     7         if len(pre) == 1:
     8             return TreeNode(pre[0])
     9         root = TreeNode(pre[0])
    10         tinL = tin[0:tin.index(pre[0])]
    11 
    12         tinR = tin[tin.index(pre[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

  • 相关阅读:
    hdu 1074
    hdu 4091
    hdu 4422
    hdu 3940
    hdu 2831
    hdu 1172
    hdu 3732
    hdu 1250
    hud 2073
    IOS socket基于tcp/udp的通信
  • 原文地址:https://www.cnblogs.com/NPC-assange/p/12123849.html
Copyright © 2011-2022 走看看