Esempio n. 1
0
 def rebuild(low, high):
     if (high - low) > 0:
         # find max val and idx
         max_val = -1
         for i in range(low, high):
             if nums[i] > max_val: max_val,max_idx = (nums[i], i)
         return TreeNode(val = max_val, left = rebuild(low, max_idx), right = rebuild(max_idx+1, high))
Esempio n. 2
0
 def rebuild(stop):
     if inorder[-1] != stop:
         root = TreeNode(val=preorder.pop())
         root.left = rebuild(root.val)
         inorder.pop()
         root.right = rebuild(stop)
         return root
Esempio n. 3
0
def tree(height):
    global i
    if height:
        root = TreeNode(val=i)
        i += 1
        root.left = tree(height-1)
        root.right = tree(height-1)
        return root
Esempio n. 4
0
 def rebuild(pl, ph, il, ih):
     if pl >= ph: return None
     cur = preorder[pl]
     # search in inorder
     idx = num2idx[cur]
     n_left = idx-il # 左子树节点数
     left = rebuild(pl+1, pl+1+n_left, il, idx)
     right = rebuild(pl+1+n_left, ph, idx+1, ih)
     return TreeNode(val=cur, left=left, right=right)
Esempio n. 5
0
 def rebuild(nums):
     if len(nums) > 0:
         # find max val and idx
         max_val = -1
         max_idx = -1
         for i,x in enumerate(nums):
             max_val,max_idx = (x, i) if x > max_val else (max_val, max_idx)
         print(nums, max_val)
         return TreeNode(val = max_val, left = rebuild(nums[:max_idx]), right = rebuild(nums[max_idx+1:]))
Esempio n. 6
0
 def rebuild(pl, ph, il, ih):
     if il >= ih: return None
     root = TreeNode(val = postorder[ph-1])
     # search in indorder
     idx = num2idx[root.val]
     mid = ph-1-(ih-1-idx)
     root.right = rebuild(mid, ph-1, idx+1, ih)
     root.left = rebuild(pl, mid, il, idx)
     return root
Esempio n. 7
0
 def rebuild(pl, ph, il, ih):
     if pl >= ph: return None
     cur = preorder[pl]
     # search in inorder
     for idx in range(il, ih):
         if inorder[idx] == cur: break
     n_left = idx-il # 左子树节点数
     left = rebuild(pl+1, pl+1+n_left, il, idx)
     right = rebuild(pl+1+n_left, ph, idx+1, ih)
     return TreeNode(val=cur, left=left, right=right)
Esempio n. 8
0
 def rebuild(pl, ph, il, ih):
     if il >= ih: return None
     root = TreeNode(val = postorder[ph-1])
     # search in indorder
     for idx in range(il, ih):
         if inorder[idx] == root.val: break
     mid = ph-1-(ih-1-idx)
     root.right = rebuild(mid, ph-1, idx+1, ih)
     root.left = rebuild(pl, mid, il, idx)
     return root
Esempio n. 9
0
 def rebuild(stop):
     if  inorder[-1] != stop:
         # 新建当前节点
         root  = TreeNode(val=postorder.pop())
         # 重构右子树,inoreder的stop为当前节点的值,找到则右子树重构完毕
         root.right = rebuild(root.val)
         # 注意右子树重构完毕,意味着inorder中已经不存在左子树的节点了
         # 那么现在该干什么?当然是pop掉当前的节点了
         inorder.pop()
         # 重构左子树,左子树的stop值为本函数的stop相同,初始为None
         root.left = rebuild(stop)
         return root
Esempio n. 10
0
 def rebuild(low, high, flag):
     if (high - low) > 0:
         # find max val and idx
         if flag:
             max_idx= dp_idx[high-1]
             max_val = nums[max_idx]
         else:
             max_val = -1
             for i in range(low, high):
                 if nums[i] > max_val: 
                     print(dp_idx[i])
                     max_val,max_idx = (nums[i], i)
                 dp_idx[i]= max_idx
         return TreeNode(val = max_val, left = rebuild(low, max_idx, True), right = rebuild(max_idx+1, high, False))
Esempio n. 11
0
 def constructMaximumBinaryTree(self, nums):
     stack = []
     for num in nums:
         # print(num, ': ', end=' ')
         # for x in stack:
         #     print(x.val, end=' ')
         # print()
         cur = TreeNode(val=num)
         # 找到比它小的做它左子节点
         while stack and stack[-1].val < cur.val:
             cur.left = stack.pop()
         # 找到比它大的最小节点x,它作为x的右子节点
         if stack:
             stack[-1].right = cur
         # cur称为右子树最末的节点,入栈
         stack.append(cur)
     return stack[0]
Esempio n. 12
0
def tree(height):
    if height:
        root = TreeNode(val=0)
        root.left = tree(height-1)
        return root
Esempio n. 13
0
 def rebuild(low, high):
     if (high - low) > 0:
         # find max val and idx
         return TreeNode(val = nums[dp_idx[low][high-1]], left = rebuild(low, dp_idx[low][high-1]), right = rebuild(dp_idx[low][high-1]+1, high))
Esempio n. 14
0
 def rebuild(nums):
     if len(nums) > 0:
         # find max val and idx
         max_idx = nums.index(max(nums))
         max_val = nums[max_idx]
         return TreeNode(val = max_val, left = rebuild(nums[:max_idx]), right = rebuild(nums[max_idx+1:]))