Code RoomLargest balanced subtree
MediumPrep Room Coding #1123

Largest balanced subtree

CodingAlgorithms & data structuresMid–Senior~30 min

Given a binary tree encoded as a level-order array (None marks a missing child), determine the largest number of nodes contained in any subtree that is height-balanced in the AVL sense, where 'height-balanced' means that for every node in that subtree the heights of its left and right children differ by at most 1. Compute it bottom-up in a single post-order pass: each node reports its height, whether its subtree is balanced, and its node count, so a parent can combine children in O(1). Return the size of the largest balanced subtree.

Implement
largest_balanced_subtree(level: list) → int
Examples
in[[1,2,2,3,3,null,null,4,4]]out5
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 30 min
InputExpectedGot
[[1,2,2,3,3,null,null,4,4]]5not run yetsample