Skip to content

Commit 16dfa68

Browse files
authored
Update tree.py
1 parent 4bac67b commit 16dfa68

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

height_of_tree/tree.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,24 @@ def tree_height_from_list(data: list[int]) -> int:
7474
>>> tree_height_from_list([5,1,10,15,7])
7575
3
7676
"""
77-
bst = BinarySearchTree()
78-
for x in data:
79-
bst.create(x)
80-
return height(bst.root)
77+
if not data:
78+
return -1
79+
nodes = [Node(val) for val in data]
80+
q = [nodes[0]]
81+
i = 1
82+
front = 0
83+
while i < len(nodes):
84+
curr = q[front]
85+
front += 1
86+
if i < len(nodes):
87+
curr.left = nodes[i]
88+
q.append(curr.left)
89+
i += 1
90+
if i < len(nodes):
91+
curr.right = nodes[i]
92+
q.append(curr.right)
93+
i += 1
94+
return height(nodes[0])
8195

8296

8397
if __name__ == "__main__":

0 commit comments

Comments
 (0)