#38093: python 紀錄


BensonDC (python戰士)

學校 : 不指定學校
編號 : 240921
來源 : [163.32.78.214]
最後登入時間 :
2024-11-06 14:27:58
m300. 12347 - Binary Search Tree -- UVa | From: [1.175.194.6] | 發表日期 : 2023-10-25 02:03

class TreeNode:
    def __init__(self,val=0,left=None,right=None):
        self.val=val
        self.left=left
        self.right=right


def insert(node,new_val):
    if not node:
        return TreeNode(new_val)
    if node.val>new_val:
        node.left=insert(node.left,new_val)
    else:
        node.right=insert(node.right,new_val)
    return node
        
tree=TreeNode(int(input()))

while True:
    try:
        n=int(input())
    except EOFError:
        break
    tree=insert(tree,n)


def post_order(tree):
    if not tree:
        return
    post_order(tree.left)
    post_order(tree.right)
    print(tree.val)
    return
post_order(tree)
    

 
ZeroJudge Forum