面朝大海,春暖华开

focus on scientific computue, 3dgis, spatial database
专注于科学计算、GIS空间分析

 

python二叉树递归求和

class BiTNode:
    def  __init__(self, data, left, right):
        self.data = data
        self.left = left
        self.right = right
t = BiTNode(10, BiTNode(2,None,None), BiTNode(3,None,None))
def sum_tree(t:BiTNode):
    total=0
    if t != None:
        total += t.data
        total += sum_tree(t.left)
        total += sum_tree(t.right)
    
    return total
sum_tree(t)

15

posted on 2020-05-21 16:55  风过 无痕  阅读(424)  评论(0编辑  收藏  举报

导航

向日葵支付宝收钱码