Q117.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //
  2. // Created by 李洋 on 2023/11/3.
  3. //
  4. #ifndef LEECODE_C_Q117_H
  5. #define LEECODE_C_Q117_H
  6. #include <stack>
  7. #include <queue>
  8. using namespace std;
  9. class Node {
  10. public:
  11. int val;
  12. Node *left;
  13. Node *right;
  14. Node *next;
  15. Node() : val(0), left(nullptr), right(nullptr), next(nullptr) {}
  16. Node(int _val) : val(_val), left(nullptr), right(nullptr), next(nullptr) {}
  17. Node(int _val, Node *_left, Node *_right, Node *_next)
  18. : val(_val), left(_left), right(_right), next(_next) {}
  19. };
  20. Node *connect(Node *root) {
  21. if (!root) {
  22. return nullptr;
  23. }
  24. queue<pair<Node *, int>> Q;
  25. Q.emplace(root, 1);
  26. while (!Q.empty()) {
  27. auto temp = Q.front();
  28. Q.pop();
  29. if (temp.second == Q.front().second) {
  30. temp.first->next = Q.front().first;
  31. }
  32. if (temp.first->left != nullptr) {
  33. Q.emplace(temp.first->left, temp.second + 1);
  34. }
  35. if (temp.first->right != nullptr) {
  36. Q.emplace(temp.first->right, temp.second + 1);
  37. }
  38. }
  39. return root;
  40. }
  41. #endif //LEECODE_C_Q117_H
备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。