File tree Expand file tree Collapse file tree 1 file changed +11
-22
lines changed
Expand file tree Collapse file tree 1 file changed +11
-22
lines changed Original file line number Diff line number Diff line change 11
2+
23class NodeTree :
34 def __init__ (self , data ):
45 self .data = data
@@ -23,6 +24,15 @@ def get_level(self):
2324 lvl += 1
2425 p = p .parent
2526 return lvl
27+
28+ def height (self ):
29+ if len (self .children ) == 0 :
30+ return 0
31+ else :
32+ max_height = - 1
33+ for child in self .children :
34+ max_height = max (max_height , child .height ())
35+ return max_height + 1
2636
2737
2838if __name__ == "__main__" :
@@ -56,25 +66,4 @@ def get_level(self):
5666 root .add_node (tv )
5767
5868 root .print_tree ()
59-
60- # print(root.get_level())
61- # print(root.children[0].get_level())
62- # print(root.children[0].children[0].get_level())
63-
64-
65- # print("\n")
66- # print(root.data)
67- # print("\t" + root.children[0].data)
68- # print("\t\t" + root.children[0].children[0].data)
69- # print("\t\t" + root.children[0].children[1].data)
70- # print("\t\t" + root.children[0].children[2].data)
71-
72- # print("\t" + root.children[1].data)
73- # print("\t\t" + root.children[1].children[0].data)
74- # print("\t\t" + root.children[1].children[1].data)
75- # print("\t\t" + root.children[1].children[2].data)
76-
77- # print("\t" + root.children[2].data)
78- # print("\t\t" + root.children[2].children[0].data)
79- # print("\t\t" + root.children[2].children[1].data)
80- # print("\t\t" + root.children[2].children[2].data)
69+ print (root .height ())
You can’t perform that action at this time.
0 commit comments