Leetcode 116. Populating Next Right Pointers in Each Node

本文介绍了一种填充二叉树中每个节点的next指针的方法,使得每个节点指向其右侧的下一个节点。提供了三种不同版本的解决方案,分别使用了两个队列、单个队列以及迭代方式实现,旨在帮助读者理解和掌握这一算法问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章作者:Tyan
博客:noahsnail.com  |  优快云  |  简书

1. Description

Populating Next Right Pointers in Each Node

2. Solution

  • Version 1
/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if(!root) {
            return;
        }
        queue<TreeLinkNode*> q1;
        queue<TreeLinkNode*> q2;
        q1.push(root);
        TreeLinkNode* pre = nullptr;
        TreeLinkNode* current = nullptr;
        while(!q1.empty()) {
            current = q1.front();
            q1.pop();
            if(current->left && current->right) {
                q2.push(current->left);
                q2.push(current->right);
            }
            if(pre) {
                pre->next = current;
            }
            pre = current;
            if(q1.empty()) {
                pre->next = nullptr;
                pre = nullptr;
                q1 = q2;
                q2 = {};
            }
        }
    }
};
  • Version 2
/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if(!root) {
            return;
        }
        queue<TreeLinkNode*> q;
        q.push(root);
        connect(q);
    }

private:
    void connect(queue<TreeLinkNode*>& q) {
        TreeLinkNode* pre = nullptr;
        TreeLinkNode* current = nullptr;
        queue<TreeLinkNode*> q2;
        while(!q.empty()) {
            current = q.front();
            q.pop();
            if(current->left && current->right) {
                q2.push(current->left);
                q2.push(current->right);
            }
            if(pre) {
                pre->next = current;
            }
            pre = current;
        }
        pre->next = nullptr;
        if(!q2.empty()) {
            connect(q2);
        }
    }
};
  • Version 3
/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if(!root) {
            return;
        }
        while(root) {
            TreeLinkNode* current = root;
            while(current && current->left) {
                current->left->next = current->right;
                if(current->next) {
                    current->right->next = current->next->left;
                }
                current = current->next;
            }
            root = root->left;
        }
    }
};

Reference

  1. https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值