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) #HERE <--------------------
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)))
all this is doing is recursively going into maxDepth until there is no more node.left or node.right, how does this return the depth??
I have commented #HERE <--- to specify where I am confused. How does this code know what value to assign lDepth and rDepth??