Given the root of a binary tree, return whether its height is balanced. That is, for every node in the tree, the absolute difference of the height of its left subtree and the height of its right subtree is 0 or
We need to make sure that the absolute difference between both the left and right side is <= 1. To do this, write a helper function that returns the height of the node: when the node is None, it returns zero, otherwise, it returns the maximum of its left and right height, which is 1 + its height applied to the same function.
For the main function, we call this helper on our current node (root), by calculating the left and right height, and making sure the absolute difference is less than or equal to 1. Then, we make sure that this property applies for all children, by calling our main function on our left and right children.
class Solution:
def height(self, node):
if not node:
return 0
= 1 + self.height(node.left)
left_height = 1 + self.height(node.right)
right_height return max(left_height, right_height)
def solve(self, root):
if not root:
return True
= self.height(root.left)
left_height = self.height(root.right)
right_height = abs(left_height - right_height) <= 1
is_valid return is_valid and self.solve(root.left) and self.solve(root.right)