Exemplo n.º 1
0
 def recursion(self, root, left, right, inorderDict, preorder):
     # step 1 : 判定 是否 到达 叶子节点
     if left > right:
         return
     # step 2 : 构建根节点
     node = Tree(preorder[root])
     # step 3 : 搜索 根节点 在 中序遍历 中 的位置
     index = inorderDict[preorder[root]]
     # step 4 : 分割 左右子树
     node.left = self.recursion(root + 1, left, index - 1, inorderDict,
                                preorder)
     node.right = self.recursion(index - left + root + 1, index + 1, right,
                                 inorderDict, preorder)
     return node
Exemplo n.º 2
0
 def left_right(left, right):
     if left > right:
         return [None]
     if (left, right) in dct:
         return dct[(left, right)]
     ret = []
     for i in range(left, right + 1):
         left_lst = left_right(left, i - 1)
         right_lst = left_right(i + 1, right)
         for L in left_lst:
             for R in right_lst:
                 app_Tree = Tree(i)
                 app_Tree.left = L
                 app_Tree.right = R
                 ret.append(app_Tree)
     dct[(left, right)] = ret
     return ret