def constructFromPrePost(self, pre: List[int],
                             post: List[int]) -> TreeNode:
        '''
        [1, 2, 3, 4, 5, 6, 7]
            1
          2   3
         4 5 6 7
         
         preorder: [1] + [2,4,5] + [3,6,7]
         postorder: [4,5,2] + [6,7,3] + [1]
         
         for each brunch, root is at the first node in preorder and last node in postorder
         if we know the root value from the preorder of the left branch,
         we go to the postorder and find the index of the root of the left branch
         we can know the length L of the left brunch
         
         so the left sub tree is pre[1:L+1], post[0:L]
         right sub tree is pre[L+1:], post[L:-1]
         
         we can use recursion to build the tree
        
        '''

        if not pre:
            return None
        root = TreeNode(pre[0])
        if len(pre) == 1:
            return root

        L = post.index(pre[1]) + 1  # length of left branch
        root.left = self.constructFromPrePost(pre[1:1 + L], post[:L])
        root.right = self.constructFromPrePost(pre[1 + L:], post[L:-1])
        return root
예제 #2
0
    def test_testCase(self):
        for Sol in [Solution1()]:
            func = Sol.distanceK
            root = TreeNode(3)
            target = TreeNode(5)
            root.left = target
            root.right = TreeNode(1)
            root.right.left = TreeNode(0)
            root.right.right = TreeNode(8)
            target.left = TreeNode(6)
            target.right = TreeNode(2)
            target.right.left = TreeNode(7)
            target.right.right = TreeNode(4)


            self.assertEqual(func(root, target, 2), [7,4,1])
예제 #3
0
def test():
    root1 = TreeNode(1)
    root2 = TreeNode(1)
    root1.left = TreeNode(2)
    root1.right = TreeNode(3)
    root1.left.left = TreeNode(4)
    root1.left.right = TreeNode(5)

    root2.left = TreeNode(2)
    root2.right = TreeNode(3)
    root2.left.left = TreeNode(4)
    root2.left.right = TreeNode(5)

    if identical_trees(root1, root2):
        print("Both trees are identical")
    else:
        print("Trees are not identical")
예제 #4
0
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root:
            return TreeNode(val, None, None)

        if val > root.val:
            root.right = self.insertIntoBST(root.right, val)
        else:
            root.left = self.insertIntoBST(root.left, val)

        return root
        def helper(nums, left, right):
            if left > right:
                return None
            mid = (left + right) // 2
            root = TreeNode(nums[mid])

            if left == right:
                return root

            root.left = helper(nums, left, mid - 1)
            root.right = helper(nums, mid + 1, right)

            return root
예제 #6
0
    def buildTreeUtils(self, preorder: List[int]) -> TreeNode:
        if not preorder:
            return None

        root = TreeNode(val=preorder[0])
        index = -1

        for i in range(1, len(preorder)):
            if preorder[i] > preorder[0]:
                index = i
                break

        if index == -1:
            root.left = self.buildTreeUtils(preorder[1:])
        else:
            root.left = self.buildTreeUtils(preorder[1:index])
            root.right = self.buildTreeUtils(preorder[index:])

        return root
예제 #7
0
    succ = None
    curr = root

    while curr.val != key:
        if curr.val > key and curr.left:
            succ = curr
            curr = curr.left
        elif curr.right:
            curr = curr.right

    if curr and curr.right:
        succ = find_min(curr.right)

    return succ.val


if __name__ == "__main__":
    root = TreeNode(9)
    root.left = TreeNode(2)
    root.right = TreeNode(10)
    root.left.left = TreeNode(1)
    root.left.right = TreeNode(7)
    root.left.right.left = TreeNode(6)
    root.left.right.right = TreeNode(8)
    root.left.right.left.left = TreeNode(5)
    # inorder(root)
    # print
    # print(inorder_predecessor(root, 7))
    # print
    print(inorder_successor_bst(root, 9))