Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Solution:
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @return a boolean def isBalanced(self, root): return self.isBalancedInt(root)>=0 def isBalancedInt(self, root): if root == None: return 0; left = self.isBalancedInt(root.left) right = self.isBalancedInt(root.right) if left<0 or right<0 or abs(left-right)>1: return -1 return max(left,right)+1
This comment has been removed by the author.
ReplyDeletenice notes..thanks for sharing with us..
ReplyDeleteonce you can check this ..
python online course
perde modelleri
ReplyDeleteSMS ONAY
VODAFONE MOBİL ÖDEME BOZDURMA
Nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
Dedektör
Websitesi Kurmak
Ask kitaplari