Documentation IndexFetch the complete documentation index at: /llms.txtUse this file to discover all available pages before exploring further.
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 110 with 15 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 110 # by problem number lcpy gen -s balanced_binary_tree # by problem name
Input: root = [3,9,20,null,null,15,7] Output: true
Input: root = [1,2,2,3,3,null,null,4,4] Output: false
Input: root = [] Output: true
[0, 5000]
-10^4 <= Node.val <= 10^4
from leetcode_py import TreeNode class Solution: # Time: O(n) # Space: O(h) def is_balanced(self, root: TreeNode[int] | None) -> bool: def height(node: TreeNode[int] | None) -> int: if not node: return 0 left = height(node.left) right = height(node.right) if left == -1 or right == -1 or abs(left - right) > 1: return -1 return max(left, right) + 1 return height(root) != -1