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
Example #2
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 invertTree(self, root: TreeNode) -> TreeNode:
        if root:
            root.left, root.right = root.right, root.left
            self.invertTree(root.left)
            self.invertTree(root.right)

            return root
    def invertTree(self, root: TreeNode) -> TreeNode:
        if not root:
            return None

        root.left, root.right = self.invertTree(root.right), self.invertTree(
            root.left)

        return root
Example #5
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
Example #6
0
    def __buildTree(self, preorder: List[int], inorder: List[int], index: int = 0) -> (TreeNode, int):
        print("preorder:", preorder, "  inorder:", inorder, "   index:", index)
        if not inorder:
            return None, index - 1

        root: TreeNode = TreeNode(val=preorder[index])
        ino_index = inorder.index(preorder[index])

        print("index:", index, "root:", root.val, "ino_index:", ino_index)

        root.left, index = self.__buildTree(preorder, inorder[:ino_index], index + 1)
        root.right, index = self.__buildTree(preorder, inorder[ino_index + 1:], index + 1)

        return root, index
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")
Example #8
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))
Example #9
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])