Ejemplo n.º 1
0
 def dfs(l, r):
     if l > r: return None
     mid = (r - l) // 2 + l
     root = TreeNode(nums[mid])
     root.left = dfs(l, mid - 1)
     root.right = dfs(mid + 1, r)
     return root
 def dfs(pl, pr, il, ir):
     if pl > pr: return None
     n = TreeNode(preorder[pl])
     if pl == pr: return n
     mid = inord_map[n.val]
     n.left = dfs(pl + 1, pl + (mid - il), il, mid - 1)
     n.right = dfs(pl + (mid - il) + 1, pr, mid + 1, il)
     return n
 def dfs(pl, pr, il, ir):
     if pl > pr: return None
     node = TreeNode(postorder[pr])
     if pl == pr: return node
     inord_mid = inord_map[node.val]
     postord_mid = pr - (ir - inord_mid)
     node.left = dfs(pl, postord_mid - 1, il, inord_mid - 1)
     node.right = dfs(postord_mid, pr - 1, inord_mid + 1, ir)
     return node