3. What will be the output of the following code snippet?
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def check_height(node):
if not node:
return 0
left_height = check_height(node.left)
right_height = check_height(node.right)
if left_height == -1 or right_height == -1 or abs(left_height - right_height) > 1:
return -1
return max(left_height, right_height) + 1
def is_balanced(root):
return check_height(root) != -1
root = TreeNode(1, TreeNode(2, TreeNode(3)), TreeNode(4))
print(is_balanced(root))