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)

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Unity Makes UI Easy

Ruby: class_eval vs module_eval

Customising Salesforce IDP Embedded Login: A Journey

SAP ABAP — Data Types

Our Agile Project Process — hmm? yet another & my problem

Clarksons’s Farm on Amazon Prime

READ/DOWNLOAD!( Frogs into Princes: Neuro Linguist

How to Choose a Cloud Communications Service Provider for Your Business

How to Choose a Cloud Communications Service Provider for Your Business

Golang: Retrieving Stock Market Data using Go Finance

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.

More from Medium

104. Maximum Depth of Binary Tree

CS373 Spring 2022: Blake Chambers — Blog 10

Needfinding

CS373 Spring 2022: Kristina Zhou