当前位置:   article > 正文

C++ | Leetcode C++题解之第117题填充每个节点的下一个右侧节点指针II

C++ | Leetcode C++题解之第117题填充每个节点的下一个右侧节点指针II

题目:

题解:

  1. class Solution {
  2. public:
  3. void handle(Node* &last, Node* &p, Node* &nextStart) {
  4. if (last) {
  5. last->next = p;
  6. }
  7. if (!nextStart) {
  8. nextStart = p;
  9. }
  10. last = p;
  11. }
  12. Node* connect(Node* root) {
  13. if (!root) {
  14. return nullptr;
  15. }
  16. Node *start = root;
  17. while (start) {
  18. Node *last = nullptr, *nextStart = nullptr;
  19. for (Node *p = start; p != nullptr; p = p->next) {
  20. if (p->left) {
  21. handle(last, p->left, nextStart);
  22. }
  23. if (p->right) {
  24. handle(last, p->right, nextStart);
  25. }
  26. }
  27. start = nextStart;
  28. }
  29. return root;
  30. }
  31. };
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/687063
推荐阅读
相关标签
  

闽ICP备14008679号