本文主要是介绍C++ | Leetcode C++题解之第116题填充每个节点的下一个右侧节点指针,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution {
public:Node* connect(Node* root) {if (root == nullptr) {return root;}// 从根节点开始Node* leftmost = root;while (leftmost->left != nullptr) {// 遍历这一层节点组织成的链表,为下一层的节点更新 next 指针Node* head = leftmost;while (head != nullptr) {// CONNECTION 1head->left->next = head->right;// CONNECTION 2if (head->next != nullptr) {head->right->next = head->next->left;}// 指针向后移动head = head->next;}// 去下一层的最左的节点leftmost = leftmost->left;}return root;}
};
这篇关于C++ | Leetcode C++题解之第116题填充每个节点的下一个右侧节点指针的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!