the art of
Algorithm
Notes on Analysis and Design



Max node in Tree

Compute the maximum node in a given tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#A binary tree node
class Node:
	def __init__(self,key):
		self.left=None
		self.right=None
		self.data=key
def findmax(root):
	if root is None:
		return 0
	res=root.data
	lres=findmax(root.left)
	rres=findmax(root.right)
	if lres>rres:
		res=lres
	else:
		res=rres
	return res
#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 findmax(root)