class Node:
def __init__(self,root=None,left=None,right=None):
self.root = root
self.left = left
self.right = right
def height(self):
return 1+max(self.left.height() if self.left is not None else 0,
self.right.height() if self.right is not None else 0)
tree = [Node() for _ in range(1023)]
root = tree[0]
for i in range(len(tree)):
l_child_idx,r_child_idx = (i+1)*2-1,(i+1)*2
root_idx = (i-1)//2
if root_idx :
tree[i].root = tree[root_idx]
if l_child_idx < len(tree):
tree[i].left = tree[l_child_idx]
if r_child_idx < len(tree):
tree[i].right = tree[r_child_idx]
print(root.height())
資料來源:https://www.796t.com/post/N2dodnk=.html
沒有留言:
張貼留言