예제 #1
0
    # inorder
    def isValidBST3(self, root: TreeNode) -> bool:
        stack = []
        pre = None
        while root or stack:
            while root:
                stack.append(root)
                root = root.left
            root = stack.pop()
            if pre and root.val <= pre.val:
                return False
            pre = root
            root = root.right
        return True


if __name__ == '__main__':
    root = TreeNode(1)
    root.left = TreeNode(5)
    a = TreeNode(4)
    root.right = a
    a.left = TreeNode(3)
    a.right = TreeNode(6)

    PrintBST.printBST(root)
    solution = Solution()
    result = solution.isValidBST3(root)
    print('Is valid BST:{}'.format(result))

    result = solution.isValidBST3(root)
    print('Is valid BST:{}'.format(result))
예제 #2
0
            node.left, node.right = node.right, node.left
            if node.left:
                stack.append(node.left)
            if node.right:
                stack.append(node.right)
        return root



if __name__ == '__main__':
    root = TreeNode(1)

    b = TreeNode(2)
    b.left = TreeNode(1)
    b.right = TreeNode(3)
    root.left = b
    a = TreeNode(7)
    root.right = a
    a.left = TreeNode(6)
    a.right = TreeNode(9)
    PrintBST.printBST(root)
    solution = Solution()
    node = solution.invertTree(root)
    PrintBST.printBST(node)
    node = solution.invertTree2(root)
    PrintBST.printBST(node)




from typing import List
from Tree.PrintBST import PrintBST


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
        if len(nums) == 0:
            return None
        mid = len(nums) // 2
        return TreeNode(val=nums[mid],
                        left=self.sortedArrayToBST(nums[:mid]),
                        right=self.sortedArrayToBST(nums[mid + 1:]))


if __name__ == '__main__':
    nums = [-10, -3, 0, 5, 9]
    solution = Solution()
    bst = solution.sortedArrayToBST(nums)

    PrintBST.printBST(bst)
예제 #4
0
    def generateTrees(self, n: int) -> List[TreeNode]:

        ans = []
        if n == 0:
            return ans

        def helper(start, end):
            ans = []
            if start > end:
                ans.append(None)
                return ans
            for i in range(start, end + 1):
                left = helper(start, i - 1)
                right = helper(i + 1, end)
                for node_l in left:
                    for node_r in right:
                        root = TreeNode(i)
                        root.left = node_l
                        root.right = node_r
                        ans.append(root)
            return ans

        return helper(1, n)


if __name__ == '__main__':
    solution = Solution()
    list_nodes = solution.generateTrees(3)
    for node in list_nodes:
        PrintBST.printBST(node)