코테준비 - Populating Next Right Pointers in Each Node

정상화·2023년 2월 26일

LeetCode

목록 보기
113/222

Populating Next Right Pointers in Each Node

class Solution {
public:
    Node *connect(Node *root) {
        if (root == nullptr) return NULL;
        auto node = root;
        auto left = node;
        Node *start = NULL;
        while (left->left) {
            while (node) {
                if (start) {
                    start->next = node->left;
                    node->left->next = node->right;
                    start = node->right;
                } else {
                    node->left->next = node->right;
                    start = node->right;
                }
                node = node->next;
            }
            node = left->left;
            left = node;
            start = NULL;
        }
        return root;
    }
};
profile
백엔드 희망

0개의 댓글