-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path662_Maximun_width_of_Binary_Tree.py
More file actions
35 lines (34 loc) · 1.1 KB
/
662_Maximun_width_of_Binary_Tree.py
File metadata and controls
35 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from collections import deque
# 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:
if root == None:
return 0
q = deque()
indexq = deque()
res = 0
q.append(root)
indexq.append(1)
while len(q) > 0:
level = len(q)
initialIndex = indexq[0]
index = initialIndex
while level > 0:
cur = q.popleft()
index = indexq.popleft()
if cur is not None:
if cur.left is not None:
q.append(cur.left)
indexq.append(index*2)
if cur.right is not None:
q.append(cur.right)
indexq.append(index*2+1)
level -= 1
width = index - initialIndex + 1
res = max(res, width)
return res