543. Diameter of Binary Tree

find_max(node):
if root ==None :
return
left_height = height_fun(node.left)
right_height =height_fun(node.right)
depth_max = max(depth_max,left_height +right_height)
find_max(node.left)
find_max(node.right)

class Solution:

def diameterOfBinaryTree(self,root):
self.max_=0
self.rec(root)
return self.max_
def rec(self,node):
if not node:
return 0
left = self.rec(node.left)
right = self.rec(node.right)
self.max_=max(self.max_,left+right)
return max(left,right)+1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.