forked from ByteByteGoHq/coding-interview-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidest_binary_tree_level.cpp
49 lines (47 loc) · 1.62 KB
/
widest_binary_tree_level.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <deque>
#include <utility>
#include <algorithm>
#include "ds/TreeNode.h"
using ds::TreeNode;
/**
* Definition of TreeNode:
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
int widestBinaryTreeLevel(TreeNode* root) {
if (!root) {
return 0;
}
int maxWidth = 0;
std::deque<std::pair<TreeNode*, int>> queue;
queue.push_back(std::make_pair(root, 0)); // Stores (node, index) pairs.
while (!queue.empty()) {
int levelSize = queue.size();
// Set the 'leftmostIndex' to the index of the first node in
// this level. Start 'rightmostIndex' at the same point as
// 'leftmostIndex' and update it as we traverse the level,
// eventually positioning it at the last node.
int leftmostIndex = queue.front().second;
int rightmostIndex = leftmostIndex;
// Process all nodes at the current level.
for (int unused = 0; unused < levelSize; unused++) {
TreeNode* node = queue.front().first;
int i = queue.front().second;
queue.pop_front();
if (node->left) {
queue.push_back(std::make_pair(node->left, 2 * i + 1));
}
if (node->right) {
queue.push_back(std::make_pair(node->right, 2 * i + 2));
}
rightmostIndex = i;
}
maxWidth = std::max(maxWidth, rightmostIndex - leftmostIndex + 1);
}
return maxWidth;
}