Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.
The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null
nodes between the end-nodes are also counted into the length calculation.
Example 1:
Input:1 / \ 3 2 / \ \ 5 3 9
Output: 4 Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
Example 2:
Input:1 / 3 / \ 5 3
Output: 2 Explanation: The maximum width existing in the third level with the length 2 (5,3).
Example 3:
Input:1 / \ 3 2 / 5
Output: 2 Explanation: The maximum width existing in the second level with the length 2 (3,2).
Example 4:
Input:1 / \ 3 2 / \ 5 9 / \ 6 7
Output: 8 Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
Note: Answer will in the range of 32-bit signed integer.
Companies:
Bloomberg, Microsoft
Related Topics:
Tree
// OJ: https://leetcode.com/problems/maximum-width-of-binary-tree/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(H)
class Solution {
typedef unsigned long long ULL;
vector<vector<ULL>> v;
ULL ans = 0;
void dfs(TreeNode *node, int lv, ULL nodeId) {
if (!node) return;
if (v.size() <= lv) v.push_back({ULLONG_MAX, 0});
v[lv][0] = min(v[lv][0], nodeId);
v[lv][1] = max(v[lv][1], nodeId);
ans = max(ans, v[lv][1] - v[lv][0] + 1);
dfs(node->left, lv + 1, 2 * nodeId);
dfs(node->right, lv + 1, 2 * nodeId + 1);
}
public:
int widthOfBinaryTree(TreeNode* root) {
if (!root) return 0;
dfs(root, 0, 0);
return ans;
}
};
// OJ: https://leetcode.com/problems/maximum-width-of-binary-tree/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
typedef unsigned long long ULL;
public:
int widthOfBinaryTree(TreeNode* root) {
if (!root) return 0;
queue<pair<TreeNode*, ULL>> q;
q.emplace(root, 0);
ULL ans = 0;
while (q.size()) {
ULL cnt = q.size(), minId, maxId;
for (int i = 0; i < cnt; ++i) {
auto p = q.front();
q.pop();
auto node = p.first;
ULL nodeId = p.second;
if (node->left) q.emplace(node->left, nodeId * 2);
if (node->right) q.emplace(node->right, nodeId * 2 + 1);
if (i == 0) minId = nodeId;
if (i == cnt - 1) maxId = nodeId;
}
ans = max(ans, maxId - minId + 1);
}
return ans;
}
};
When there is only one node in this level, reset the id
to be 0
.
// OJ: https://leetcode.com/problems/maximum-width-of-binary-tree/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
int widthOfBinaryTree(TreeNode* root) {
if (!root) return 0;
queue<pair<TreeNode*, int>> q;
q.emplace(root, 0);
int ans = 0;
while (q.size()) {
int cnt = q.size();
if (cnt == 1) {
q.emplace(q.front().first, 0);
q.pop();
}
ans = max(ans, q.back().second - q.front().second + 1);
while (cnt--) {
root = q.front().first;
int id = q.front().second;
q.pop();
if (root->left) q.emplace(root->left, 2 * id);
if (root->right) q.emplace(root->right, 2 * id + 1);
}
}
return ans;
}
};