class Solution {
public int widthOfBinaryTree(TreeNode root) {
List<Pair<TreeNode,Integer>> arr = new ArrayList<>();
int res = 0;
arr.add(new Pair<TreeNode,Integer>(root,1));
while (!arr.isEmpty()){
List<Pair<TreeNode,Integer>> tmp = new ArrayList<>();
for (Pair<TreeNode, Integer> pair : arr) {
TreeNode node = pair.getKey();
Integer index = pair.getValue();
if(node.left != null){
tmp.add(new Pair<>(node.left,index * 2));
}
if(node.right != null){
tmp.add(new Pair<>(node.right,index * 2 + 1));
}
}
res = Math.max(arr.get(arr.size() - 1).getValue() - arr.get(0).getValue() + 1,res);
arr = tmp;
}
return res;
}
}