Max Number of Bends in Tree
Given a tree find maximum number of bends that a path can form
class TreeNode(object):
def __init__(self,data):
self.value = data
self.left = None
self.right = None
def find_path_with_max_bends(root):
if not root:
return 0
left = _path_with_max_bends(root.left,'l')
right = _path_with_max_bends(root.right,'r')
return max(left,right)-1
def _path_with_max_bends(root,bend):
if not root:
return 0
left_bends = _path_with_max_bends(root.left,'l')
right_bends = _path_with_max_bends(root.right,'r')
if bend == 'r':
return max(1+left_bends,right_bends)
else:
return max(left_bends,1+right_bends)