Maximum Width of Binary Tree - LeetCode 662 - Python Solution

preview_player
Показать описание
Solution Blog: (sign into leetcode to view)

1. Write out the templated iterative BFS code.

2. Update the logic to track the "node index". Start at 1, for the left child do i * 2 and the right child i * 2 + 1.

3. Finally to get the width of every level just take the "node index" of the rightmost node and subtract it by the leftmost node's "node index" and add 1!

Time complexity: O(N)
Space complexity: O(N)
Рекомендации по теме
join shbcf.ru