104. Maximum Depth of Binary Tree

class Solution:
def maxDepth(self, root: TreeNode) -> int:
return self.rec(root)
def rec(self,root):
if not root:
return 0
left=self.rec(root.left)
right=self.rec(root.right)
return max(left ,right)+1
class Solution:
def maxDepth(self, root: TreeNode) -> int:
q=[]
counter=0
if root:
q=[root]
while(q):
size = len(q)
for i in range(size):
if q[0].left:
q.append(q[0].left)
if q[0].right:
q.append(q[0].right)

q.pop(0)
counter+=1
return counter

--

--

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.