Source: https://leetcode.com/problems/maximum-width-of-binary-tree/

Given the root of a binary tree, return the maximum width of the given tree. The maximum width of a tree is the maximum width among all levels. The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation. It is guaranteed that the answer will in the range of a 32-bit signed integer.

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
// DFS Approach
// O(N) time, O(N) space
var widthOfBinaryTree = function(root) {
// Initialize overallMaxWidth to 0 (use BigInt for 32 bit overflow)
let overallMaxWidth = 0n;
// Initialize hashmap of depth to first column index of node at that depth
const firstColumnIndexMap = new Map();
// DFS to go through all the nodes and compute the max width along the way by keeping track of the first column index
// we've seen at a certain level in our map and the current column index of a node
const dfs = (node, depth, colIndex) => {
if (node === null) {
return;
}
// If we haven't seen a node yet at the current depth, we will add the first colIndex to the map for that depth
if (!firstColumnIndexMap.has(depth)) {
firstColumnIndexMap.set(depth, colIndex);
}
// Get the first column index at the current depth
const firstColIndex = firstColumnIndexMap.get(depth);
// Compute the current max width by doing colIndex - firstColIndex + 1
const currentMaxWidth = colIndex - firstColIndex + 1n;
overallMaxWidth = Math.max(Number(overallMaxWidth), Number(currentMaxWidth));
// Recursively go through the next depth and the left/right subtrees
dfs(node.left, depth + 1, 2n * colIndex);
dfs(node.right, depth + 1, 2n * colIndex + 1n);
};
dfs(root, 0, 0n);
return overallMaxWidth;
};