Ejemplo n.º 1
0
def loop_main(temp):
    temp = re.sub('#.*', "", temp)
    temp = re.sub('//.*', "", temp)
    if len(temp) == 0:
        return
        
    flds = temp.replace("\"","").replace("[","").replace("]","").rstrip()

    ope_t = OperateTreeNode()
    codec = Codec2()

    if len(flds) > 0:
        root = codec.deserialize(flds)
      # root = ope_t.createTreeNode(flds)
    else:
        root = None
    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))

    time0 = time.time()

    root = codec.deserialize(flds)
    result = codec.serialize(root)

    time1 = time.time()

    print("result = [{0}]".format(result))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
Ejemplo n.º 2
0
def loop_main(temp):
    temp = re.sub('#.*', "", temp)
    temp = re.sub('//.*', "", temp)
    if len(temp) == 0:
        return

    flds = temp.replace("\"", "").replace("[[",
                                          "").replace("]]",
                                                      "").rstrip().split("],[")

    ope_t = OperateTreeNode()
    root = ope_t.createTreeNode(flds[0])
    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))

    k = int(flds[1])
    print("k = {0:d}".format(k))

    sl = Solution()
    time0 = time.time()

    result = sl.kthSmallest(root, k)

    time1 = time.time()
    print("result = {0:d}".format(result))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
Ejemplo n.º 3
0
def loop_main(temp):
    flds = temp.replace("\"","").replace("[","").replace("]","").rstrip()
    n = int(flds)
    print("n = {0:d}".format(n))

    sl = Solution()
    time0 = time.time()

    result = sl.allPossibleFBT(n)

    time1 = time.time()

    cd = Codec()
    print("result = [")
    for i, node in enumerate(result):
        if i == 0:
            print(" [{0}]".format(cd.serialize(node)))
        else:
            print(",[{0}]".format(cd.serialize(node)))
    print("]")

    ope_t = OperateTreeNode()
    for i, node in enumerate(result):
        print("result[{0:d}] = \n{1}".format(i, ope_t.treeToStaircaseString(node)))
    print()
    for i, node in enumerate(result):
        print("result[{0:d}] = {1}".format(i, ope_t.tree2str(node)))
    print()

    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
def loop_main(temp):
    flds = temp.replace("\"", "").replace("[", "").replace("]", "").rstrip()
    nums = [int(n) for n in flds.split(",")]
    print("nums = {0}".format(nums))

    sl = Solution()
    time0 = time.time()
    result = sl.sortedArrayToBST(nums)

    time1 = time.time()

    ope_t = OperateTreeNode()
    print("result = \n{0}".format(ope_t.treeToStaircaseString(result)))
    print("result = {0}".format(ope_t.tree2str(result)))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
def loop_main(temp):
    flds = temp.replace("\"","").replace("[","").replace("]","").rstrip()

    ope_t = OperateTreeNode()
    root = ope_t.createTreeNode(flds)
    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))

    sl = Solution()
    time0 = time.time()

    result = sl.diameterOfBinaryTree(root)

    time1 = time.time()
    print("result = {0}".format(result))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
Ejemplo n.º 6
0
def loop_main(temp):
    n = int(temp.replace("[", "").replace("]", "").rstrip())
    print("num = {0:d}".format(n))

    sl = Solution()
    time0 = time.time()

    result = sl.generateTrees(n)

    time1 = time.time()

    ope_t = OperateTreeNode()
    for i, res in enumerate(result):
        print("result[{0:d}] = \n{1}".format(
            i, ope_t.treeToStaircaseString(result[i])))
        print("result[{0:d}] = {1}".format(i, ope_t.tree2str(result[i])))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
Ejemplo n.º 7
0
def loop_main(temp):
    flds = temp.replace("\"", "").replace("[", "").replace("]", "").rstrip()

    preorder = [int(fld) for fld in flds.split(",")]
    print("preorder = {0}".format(preorder))

    sl = Solution()
    time0 = time.time()

    result = sl.bstFromPreorder(preorder)

    time1 = time.time()

    ope_t = OperateTreeNode()
    print("result = \n{0}".format(ope_t.treeToStaircaseString(result)))
    print("result = {0}".format(ope_t.tree2str(result)))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
Ejemplo n.º 8
0
def loop_main(temp):
    flds = temp.replace("\"", "").replace("[[",
                                          "").replace("]]",
                                                      "").rstrip().split("],[")

    ope_t = OperateTreeNode()
    root = ope_t.createTreeNode(flds[0])
    target = int(flds[1])

    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))
    print("target = {0:d}\n".format(target))

    sl = Solution()
    time0 = time.time()

    result = sl.removeLeafNodes(root, target)

    time1 = time.time()

    cd = Codec()

    print("result = \n{0}".format(ope_t.treeToStaircaseString(result)))
    print("result = {0}".format(ope_t.tree2str(result)))
    print("result = [{0}]".format(cd.serialize(result)))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
def loop_main(temp):
    flds = temp.replace("\"", "").replace("[[",
                                          "").replace("]]",
                                                      "").rstrip().split("],[")

    ope_t = OperateTreeNode()

    root = ope_t.createTreeNode(flds[0])
    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))

    L = int(flds[1])
    R = int(flds[2])
    print("L = {0:d}, R = {1:d}".format(L, R))

    sl = Solution()
    time0 = time.time()

    result = sl.trimBST(root, L, R)

    time1 = time.time()

    print("result = \n{0}".format(ope_t.treeToStaircaseString(result)))
    print("result = {0}".format(ope_t.tree2str(result)))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
def loop_main(temp):
    flds = temp.replace("\"", "").replace("[[",
                                          "").replace("]]",
                                                      "").rstrip().split("],[")

    preorder = [int(n) for n in flds[0].split(",")]
    inorder = [int(n) for n in flds[1].split(",")]
    print("preorder = {0}, inorder = {1}".format(preorder, inorder))

    sl = Solution()
    time0 = time.time()

    result = sl.buildTree(preorder, inorder)

    time1 = time.time()

    ope_t = OperateTreeNode()
    print("result = \n{0}".format(ope_t.treeToStaircaseString(result)))
    print("result = {0}".format(ope_t.tree2str(result)))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
Ejemplo n.º 11
0
def loop_main(temp):
    temp = re.sub('#.*', "", temp)
    temp = re.sub('//.*', "", temp)
    if len(temp) == 0:
        return

    flds = temp.replace("\"", "").replace("[", "").replace("]", "").rstrip()

    ope_t = OperateTreeNode()
    root = ope_t.createTreeNode(flds)
    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))

    sl = Solution()
    time0 = time.time()

    result = sl.postorderTraversal(root)

    time1 = time.time()
    print("result = {0}".format(result))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
def loop_main(temp):
    flds = temp.replace("\"", "").replace("[", "").replace("]", "").rstrip()

    ope_t = OperateTreeNode()
    if len(flds) > 0:
        root = ope_t.createTreeNode(flds)
    else:
        root = None

    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))

    sl = Solution()
    time0 = time.time()

    result = sl.levelOrderBottom(root)

    time1 = time.time()

    print("result = {0}".format(result))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
def loop_main(temp):
    flds = temp.replace("\"", "").replace(", ", ",").rstrip().split("],[[[")
    cmds = flds[0].replace("[[", "").split(",")

    node_flds = (flds[1].split("]],["))[0]
    ope_t = OperateTreeNode()

    if len(node_flds) > 0:
        mynode = ope_t.createTreeNode(node_flds)
    else:
        mynode = None

    print("mynode = \n[{0}]".format(ope_t.treeToStaircaseString(mynode)))

    sl = Solution()
    time0 = time.time()

    sl.main(cmds, mynode)

    time1 = time.time()

    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
Ejemplo n.º 14
0
def loop_main(temp):
    str_args = temp.replace("\"", "").replace("[[", "").replace("]]",
                                                                "").rstrip()
    flds = str_args.split("],[")

    ope_t = OperateTreeNode()
    root = ope_t.createTreeNode(flds[0])
    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))

    sum = int(flds[1].replace("sum = ", ""))
    print("sum = {0:d}".format(sum))

    sl = Solution()
    time0 = time.time()

    result = sl.pathSum(root, sum)

    print("result = {0:d}".format(result))

    time1 = time.time()

    print("result = {0:d}".format(result))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
Ejemplo n.º 15
0
def loop_main(temp):
    str_args = temp.replace("\"", "").replace("[[", "").replace("]]",
                                                                "").rstrip()
    flds = str_args.split("],[")

    ope_t = OperateTreeNode()
    root = ope_t.createTreeNode(flds[0])
    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))

    p = set_target_node(root, int(flds[1]))
    q = set_target_node(root, int(flds[2]))
    print("p = {0}, q = {1}".format(ope_t.tree2str(p), ope_t.tree2str(q)))

    sl = Solution()
    time0 = time.time()

    result = sl.lowestCommonAncestor(root, p, q)

    time1 = time.time()
    print("result = \n{0}".format(ope_t.treeToStaircaseString(result)))
    print("result = {0}\n".format(ope_t.tree2str(result)))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
def loop_main(temp):
    str_args = temp.replace("\"", "").replace("[[", "").replace("]]",
                                                                "").rstrip()
    flds = str_args.split("],[")

    ope_t = OperateTreeNode()
    t1 = ope_t.createTreeNode(flds[0])
    t2 = ope_t.createTreeNode(flds[1])
    print("t1 = \n{0}".format(ope_t.treeToStaircaseString(t1)))
    print("t1 = {0}".format(ope_t.tree2str(t1)))
    print("t2 = \n{0}".format(ope_t.treeToStaircaseString(t2)))
    print("t2 = {0}".format(ope_t.tree2str(t2)))

    sl = Solution()
    time0 = time.time()

    result = sl.mergeTrees(t1, t2)

    time1 = time.time()

    print("result = \n{0}".format(ope_t.treeToStaircaseString(result)))
    print("result = {0}".format(ope_t.tree2str(result)))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
def loop_main(temp):
    flds = temp.replace("\"","").replace("[","").replace("]","").rstrip()

    ope_t = OperateTreeNode()
    root = ope_t.createTreeNode(flds)
    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))

    sl = Solution()
    time0 = time.time()

    result = sl.findDuplicateSubtrees(root)

    time1 = time.time()

    ope_t = OperateTreeNode()
    print("result = [", end = "")
    for i, node in enumerate(result):
        if i == 0:
            print(" [{0}]".format(ope_t.tree2str(node)), end = "")
        else:
            print(" ,[{0}]".format(ope_t.tree2str(node)), end = "")
    print(" ]")
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
def loop_main(temp):
    str_args = temp.replace("\"","").replace("[[","").replace("]]","").rstrip()
    flds = str_args.split("],[")

    ope_t = OperateTreeNode()

    s = ope_t.createTreeNode(flds[0])
    print("s = \n{0}".format(ope_t.treeToStaircaseString(s)))
    print("s = {0}".format(ope_t.tree2str(s)))

    t = ope_t.createTreeNode(flds[1])
    print("t = \n{0}".format(ope_t.treeToStaircaseString(t)))
    print("t = {0}".format(ope_t.tree2str(t)))

    sl = Solution()
    time0 = time.time()
    result = sl.isSubtree(s, t)

    time1 = time.time()

    print("result = {0}".format(result))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
Ejemplo n.º 19
0
def loop_main(temp):
    flds = temp.replace("\"","").replace("[[","").replace("]]","").rstrip().split("],[")

    ope_t = OperateTreeNode()

    root1 = ope_t.createTreeNode(flds[0])
    print("root1 = \n{0}".format(ope_t.treeToStaircaseString(root1)))
    print("root1 = {0}".format(ope_t.tree2str(root1)))

    root2 = ope_t.createTreeNode(flds[1])
    print("root2 = \n{0}".format(ope_t.treeToStaircaseString(root2)))
    print("root2 = {0}".format(ope_t.tree2str(root2)))

    sl = Solution()
    time0 = time.time()

    result = sl.leafSimilar(root1, root2)

    time1 = time.time()

    print("result = {0}".format(result))
    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))
def loop_main(temp):
    flds = temp.replace("\"", "").replace("[[",
                                          "").replace("]]",
                                                      "").rstrip().split("],[")

    ope_t = OperateTreeNode()
    root = ope_t.createTreeNode(flds[0])
    to_delete = [int(n) for n in flds[1].split(",")]

    print("root = \n{0}".format(ope_t.treeToStaircaseString(root)))
    print("root = {0}".format(ope_t.tree2str(root)))
    print("to_detele = {0}\n".format(to_delete))

    sl = Solution()
    time0 = time.time()

    result = sl.delNodes(root, to_delete)

    time1 = time.time()

    cd = Codec()
    for i, node in enumerate(result):
        if i == 0:
            print("result = [[{0}]".format(cd.serialize(node)), end="")
        else:
            print(",[{0}]".format(cd.serialize(node)), end="")
    print("]\n")

    for i, node in enumerate(result):
        print("result[{0:d}] = \n{1}".format(
            i, ope_t.treeToStaircaseString(node)))
    print()
    for i, node in enumerate(result):
        print("result[{0:d}] = {1}".format(i, ope_t.tree2str(node)))
    print()

    print("Execute time ... : {0:f}[s]\n".format(time1 - time0))