leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0116.cpp (556B)


      1 class Solution {
      2   public:
      3     Node *connect(Node *root) {
      4         if (!root) return {};
      5 
      6         queue<Node *> q;
      7         q.push(root);
      8         for (int lvl = 0; !q.empty(); lvl++) {
      9             Node *prev = nullptr;
     10             for (int t = q.size(); t > 0; t--) {
     11                 Node *root = q.front();
     12                 q.pop();
     13                 root->next = prev;
     14                 prev = root;
     15                 if (root->right) q.push(root->right);
     16                 if (root->left) q.push(root->left);
     17             }
     18         }
     19         return root;
     20     }
     21 };