leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0116.cpp (556B)
0 class Solution {
1 public:
2 Node *connect(Node *root) {
3 if (!root) return {};
5 queue<Node *> q;
6 q.push(root);
7 for (int lvl = 0; !q.empty(); lvl++) {
8 Node *prev = nullptr;
9 for (int t = q.size(); t > 0; t--) {
10 Node *root = q.front();
11 q.pop();
12 root->next = prev;
13 prev = root;
14 if (root->right) q.push(root->right);
15 if (root->left) q.push(root->left);
16 }
17 }
18 return root;
19 }
20 };