MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / height

Function height

data_structures/binary_tree/binary_tree_traversals.py:85–93  ·  view source on GitHub ↗

Recursive function for calculating the height of the binary tree. >>> height(None) 0 >>> height(make_tree()) 3

(root: Node | None)

Source from the content-addressed store, hash-verified

83
84
85def height(root: Node | None) -> int:
86 """
87 Recursive function for calculating the height of the binary tree.
88 >>> height(None)
89 0
90 >>> height(make_tree())
91 3
92 """
93 return (max(height(root.left), height(root.right)) + 1) if root else 0
94
95
96def level_order(root: Node | None) -> Generator[int]:

Callers 2

zigzagFunction · 0.85
mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected