Height of Binary Tree Python
PROGRAM TO FIND THE MAXIMUM DEPTH OR HEIGHT OF A TREE
OUTPUT:
Height of tree is 2
CREDITS: https://www.geeksforgeeks.org/write-a-c-program-to-find-the-maximum-depth-or-height-of-a-tree/
class
Node:
# Constructor to create a new node
def
__init__(
self
, data):
self
.data
=
data
self
.left
=
None
self
.right
=
None
# Compute the "maxDepth" of a tree -- the number of nodes
# along the longest path from the root node down to the
# farthest leaf node
def
maxDepth(node):
if
node
is
None
:
return
0
;
else
:
# Compute the depth of each subtree
lDepth
=
maxDepth(node.left)
rDepth
=
maxDepth(node.right)
# Use the larger one
if
(lDepth > rDepth):
return
lDepth
+
1
else
:
return
rDepth
+
1
# Driver program to test above function
root
=
Node(
1
)
root.left
=
Node(
2
)
root.right
=
Node(
3
)
root.left.left
=
Node(
4
)
root.left.right
=
Node(
5
)
print
(
"Height of tree is %d"
%
(maxDepth(root)))
Height of tree is 2
Comments
Post a Comment