# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
cur_size = 1
next_size = 0
queue = [[root, 1]]
max_width = 1
while queue:
level = []
for _ in range(cur_size):
node, index = queue.pop(0)
level.append(index)
if node.left:
queue.append([node.left, index * 2])
next_size += 1
if node.right:
queue.append([node.right, index * 2 + 1])
next_size += 1
cur_size = next_size
next_size = 0
max_width = max(max_width, level[-1] - level[0] + 1)
print(level)
return max_width
标签:node,right,Python,max,self,next,宽度,二叉树,size
From: https://www.cnblogs.com/DCFV/p/18426576