class Solution:
    def convertBST(self, root: TreeNode) -> TreeNode:
        if not root:
            return []
        self.t = 0

        def process(root):
            if not root:
                return 0
            process(root.right)
            root.val = root.val + self.t
            self.t = root.val
            process(root.left)
            return root.val

        process(root)
        return root


if __name__ == "__main__":
    # root = TreeNode(5)
    # root.left = TreeNode(2)
    # root.right = TreeNode(13)
    root = TreeNode(2)
    root.right = TreeNode(3)
    root.left = TreeNode(0)
    root.left.left = TreeNode(-4)
    root.left.right = TreeNode(1)
    from 二叉树_打印二叉树 import printTree
    printTree(Solution().convertBST(root))
        fromNode.right = preNode
        preNode = fromNode
        fromNode = nextNode
    return preNode


if __name__ == "__main__":
    b = TreeNode(1)
    b.left = None
    b.right = TreeNode(2)
    b.right.left = TreeNode(3)
    b.right.right = TreeNode(4)
    b.right.left.left = TreeNode(5)

    from 二叉树_打印二叉树 import printTree
    printTree(b)

    from 二叉树的前序遍历 import Solution
    ans = Solution().preorderTraversal(b)
    print(ans)
    morrisPre(b)

    from 二叉树的中序遍历 import Solution
    ans = Solution().inorderTraversal(b)
    print(ans)
    morrisIn(b)

    from 二叉树的后序遍历 import Solution
    ans = Solution().postorderTraversal(b)
    print(ans)
    morrisPos(b)
    minVal = min(
        min(leftInfo.min if leftInfo else math.inf,
            rightInfo.min if rightInfo else math.inf), x.val)

    maxBSTSize = 0
    if leftInfo != None:
        maxBSTSize = max(maxBSTSize, leftInfo.maxBSTSize)
    if rightInfo != None:
        maxBSTSize = max(maxBSTSize, leftInfo.maxBSTSize)

    isBST = False
    if (True if leftInfo == None else leftInfo.isBST and True
            if rightInfo == None else rightInfo.isBST and True if leftInfo ==
            None else leftInfo.max < x.val and True if rightInfo ==
            None else rightInfo.min > x.val):
        maxBSTSize = (0 if leftInfo== None else leftInfo.maxBSTSize) + \
        (0 if rightInfo== None else rightInfo.maxBSTSize) + 1

    return Info(maxBSTSize, isBST, maxVal, minVal)


if __name__ == "__main__":
    from 二叉树_打印二叉树 import printTree
    from 二叉树_序列化与反序列化 import Codec
    t_s = '5_2_1_#_#_3_#_#_8_7_#_#_10_#_#_'
    t = Codec().deserialize(t_s)
    printTree(t)

    print(process(t).maxBSTSize)
示例#4
0
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


def buildTree(arr, L, R):
    if L > R:
        return None
    head = TreeNode(arr[R])
    if L == R:
        return head
    # arr[L..R-1] < arr[R] L, M
    #             > arr[R] M+1, R-1
    M = R - 1
    for i in range(L, R):
        if arr[i] > arr[R]:
            M = i - 1
            break
    head.left = buildTree(arr, L, M)
    head.right = buildTree(arr, M + 1, R - 1)
    return head


from 二叉树_打印二叉树 import printTree

arr = [1, 3, 2, 6, 8, 7, 5]
N = len(arr)
printTree(buildTree(arr, 0, N - 1))
        def process(root):
            if not root:
                return
            process(root.left)
            if root.val in dic:
                dic[root.val] += 1
            else:
                dic[root.val] = 1
            process(root.right)

        process(root)
        ret = []
        max_count = max(list(dic.values()))
        for key, value in dic.items():
            if value == max_count:
                ret.append(key)
        return ret


if __name__ == "__main__":
    from 二叉树_打印二叉树 import printTree
    head = TreeNode(1)
    head.right = TreeNode(2)
    head.right.left = TreeNode(2)
    head.right.right = TreeNode(3)
    head.right.right.left = TreeNode(3)
    # head = TreeNode(0)
    # head.right = TreeNode(0)
    printTree(head)
    print(Solution().findMode(head))
        :type data: str
        :rtype: TreeNode
        
        输入先序遍历字符串
        """
        return deserializeByPre(data)

# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))

if __name__ == "__main__":
    a = "1_1_#_1_#_#_#_"
    codec = Codec()
    head = codec.deserialize(a)
    printTree(head)
    print(codec.serialize(head))

    b = ['1','2','#','3','4','#','#','#','#']
    head1 = deserializeFromListByPre(b)
    printTree(head1)

    b2 = ['1','2','#','3']

    head2 = deserializeFromListByLevel(b2)
    printTree(head2)

    b3 = '1_2_#_3'
    head3 = deserializeByLevel(b3)
    printTree(head3)