Пример #1
0
    def sortedListToBST(self, head):
        """
        :type head: ListNode
        :rtype: TreeNode
        """
        if head is None:
            return None
        elif head.next is None:
            return TreeNode(head.val)

        total_count = 0

        cur_node = head
        while cur_node is not None:
            cur_node = cur_node.next
            total_count += 1

        mid_count = int(total_count / 2)

        cur_node = head
        while mid_count > 1:
            cur_node = cur_node.next
            mid_count -= 1

        root_tree_node = TreeNode(cur_node.next.val)
        right_head = cur_node.next.next
        cur_node.next = None
        root_tree_node.left = self.sortedListToBST(head)
        root_tree_node.right = self.sortedListToBST(right_head)

        return root_tree_node
Пример #2
0
    def preOrderDes(self, items):
        if len(items) == 0:
            return None
        item = items.pop(0)
        if item == "#":
            return None
        else:
            root = TreeNode(int(item))

            root.left = self.preOrderDes(items)
            root.right = self.preOrderDes(items)
            return root
    def sortedArrayToBST(self, nums):
        """
        :type nums: List[int]
        :rtype: TreeNode
        """
        if len(nums) == 0:
            return None
        left = 0
        right = len(nums) - 1
        mid = left + (right - left) / 2

        root_node = TreeNode(nums[mid])

        root_node.left = self.sortedArrayToBST(nums[:mid])
        root_node.right = self.sortedArrayToBST(nums[mid + 1:])
        return root_node
    def backTrack(self, start, end):
        root_list = []
        if start > end:
            root_list.append(None)
            return root_list

        for index in range(start, end + 1):
            left_root_list = self.backTrack(start, index - 1)
            right_root_list = self.backTrack(index + 1, end)

            for left_root in left_root_list:
                for right_root in right_root_list:
                    new_root = TreeNode(index)
                    new_root.left = left_root
                    new_root.right = right_root
                    root_list.append(new_root)

        return root_list
Пример #5
0
    def buildTree(self, inorder, postorder):
        """
        :type inorder: List[int]
        :type postorder: List[int]
        :rtype: TreeNode
        """
        if len(inorder) == 0:
            return None

        root_val = postorder[-1]
        root_val_index = -1
        for index, val in enumerate(inorder):
            if val == root_val:
                root_val_index = index
                break

        root_node = TreeNode(root_val)
        root_node.left = self.buildTree(inorder[:root_val_index],
                                        postorder[:root_val_index])
        root_node.right = self.buildTree(inorder[root_val_index + 1:],
                                         postorder[root_val_index:-1])
        return root_node
Пример #6
0
                pre = cur.right

            while pre.left is None and pre.right is None:
                pre = pre.next

            level += 1

    def connect_recursive(self, root):
        if root:
            return
        dummy = TreeNode(-1)
        pre = dummy
        cur = root
        while cur:
            if cur.left:
                pre.next = cur.left
                pre = cur.left
            if cur.right:
                pre.next = cur.right
                pre = cur.right
            cur = cur.next
        self.connect_recursive(dummy.next)

if __name__ == '__main__':
    node1 = TreeNode(1)
    node2 = TreeNode(2)
    node1.left = node2

    solution = Solution()
    solution.connect(node1)