小编xar*_*ena的帖子

在 Python 中对 n 元树的所有节点求和

我有一个来自此链接的 excel 文件中的表格:

在此处输入图片说明

如果Input值是1s,我怎么能把它读成一个 n 元树并总结 Python 中的所有节点?如果它nodesleaves名称也显示在树上会更好。

         15
     /        \
    8          7
  /  \      /     \
  6   2    2       5
/ | \ |   /  \   / | \
3 3 0 2   2  0  0  2  3 
Run Code Online (Sandbox Code Playgroud)

非常感谢您在 adavance 的帮助。

我的试用代码:

class Node:
    def __init__(self, name, weight, children):
        self.children = children
        self.weight = weight
        self.weight_plus_children = weight

    def get_all_weight(self):
        if self.children is None:
            return self.weight_plus_children
        else:
            for child …
Run Code Online (Sandbox Code Playgroud)

python algorithm recursion binary-tree python-3.x

1
推荐指数
1
解决办法
677
查看次数

标签 统计

algorithm ×1

binary-tree ×1

python ×1

python-3.x ×1

recursion ×1