We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0a76d92 commit c70ca6dCopy full SHA for c70ca6d
DataStructure/Tree/tree.py
@@ -29,6 +29,8 @@ def print_tree(self):
29
return
30
31
def print_func(node, depth):
32
+ if node is None:
33
+ return
34
if depth == 0:
35
print(' ' * depth + str(node.key))
36
else:
@@ -38,7 +40,24 @@ def print_func(node, depth):
38
40
39
41
print_func(self.root, 0)
42
43
+ def size(self):
44
+ def inner_size(node):
45
+ """
46
+ 子树的大小
47
+ :param node:
48
+ :return:
49
50
+ size_num = 0
51
52
+ return size_num
53
+ for child in node.children:
54
+ size_num += inner_size(child)
55
+ return size_num + 1
56
+
57
+ return inner_size(self)
58
-root = Tree(1, [Tree(2, [Tree(3, [])]), Tree(4, [])])
-print(root.depth())
-root.print_tree()
59
+if __name__ == '__main__':
60
+ root = Tree(1, [Tree(2, [Tree(3, [])]), Tree(4, [])])
61
+ print(root.depth())
62
+ root.print_tree()
63
+ print(root.children[0].size())
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments