110. Balanced Binary Tree

check(node):
if root
return true
left_height = height_find(node.left)+1
right_height =height_find(node.right)+1
if abs(left_height -right_height) >1
return false
left = check(node.left)
right = check(node.right)

if left ==false or right == false
return false
return true

class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if self.rec(root) ==-1:
return False
return True

def rec(self,root):
if not root:
return 0
left = self.rec(root.left)
right=self.rec(root.right)

if left==-1 or right == -1 or abs(right-left)>1 :
return -1
return 1 + max(left,right)

--

--

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.