2024年1月11日 星期四

leaves .. python

 tree1 = {

    1: [2, 3],

    2: [4, 5],

    3: [6, None],

    4: [None,None],

    5: [None, None],

    6: [None, None]

}


def leaves(tree,node):

    if node is not None:

        left_child, right_child = tree[node]

        

        if left_child is None and right_child is None:

            print(node, end = ' ')

        

        leaves(tree,left_child)

        leaves(tree,right_child)

        

leaves(tree1,1)

 

沒有留言:

張貼留言