LeetCode //C - 117. Populating Next Right Pointers in Each Node II

117. Populating Next Right Pointers in Each Node II

Given a binary tree

struct Node {

int val;

Node *left;

Node *right;

Node *next;

}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL .

Example 1:

Input: root = [1,2,3,4,5,null,7]
Output: [1,#,2,3,#,4,5,7,#]
Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.

Example 2:

Input: root = []
Output: []

Constraints:

  • The number of nodes in the tree is in the range [0, 6000].
  • -100 <= Node.val <= 100

Follow-up:

  • You may only use constant extra space.
  • The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.

From: LeetCode

Link: 117. Populating Next Right Pointers in Each Node II


Solution:

Ideas:

The approach we will follow is:

  1. Traverse the tree level by level (Breadth First Search).
  2. For each level, go through each node and set the next pointer to the next node in that level.
  3. If there's no next node in that level, set the next pointer to NULL.
Code:
c 复制代码
/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     struct Node *left;
 *     struct Node *right;
 *     struct Node *next;
 * };
 */

struct Node* connect(struct Node* root) {
    if (!root) return NULL;

    struct Node* prev = NULL;     // Previous node in the current level
    struct Node* head = NULL;     // Head node of the next level
    struct Node* curr = root;     // Current node of the current level

    while (curr) {
        while (curr) {
            // Process the left child
            if (curr->left) {
                if (prev) {
                    prev->next = curr->left;
                } else {
                    head = curr->left;
                }
                prev = curr->left;
            }

            // Process the right child
            if (curr->right) {
                if (prev) {
                    prev->next = curr->right;
                } else {
                    head = curr->right;
                }
                prev = curr->right;
            }

            // Move to the next node in the current level
            curr = curr->next;
        }

        // Move to the next level
        curr = head;
        head = NULL;
        prev = NULL;
    }

    return root;
}
相关推荐
IT猿手35 分钟前
2025最新群智能优化算法:海市蜃楼搜索优化(Mirage Search Optimization, MSO)算法求解23个经典函数测试集,MATLAB
开发语言·人工智能·算法·机器学习·matlab·机器人
马剑威(威哥爱编程)38 分钟前
C语言操作MySQL从入门到精通
c语言·mysql·adb
IT猿手3 小时前
2025最新群智能优化算法:山羊优化算法(Goat Optimization Algorithm, GOA)求解23个经典函数测试集,MATLAB
人工智能·python·算法·数学建模·matlab·智能优化算法
Dream it possible!6 小时前
LeetCode 热题 100_字符串解码(71_394_中等_C++)(栈)
c++·算法·leetcode
Kurbaneli7 小时前
深入理解 C 语言函数的定义
linux·c语言·ubuntu
Archer1947 小时前
C语言——链表
c语言·开发语言·链表
修己xj7 小时前
算法系列之深度优先搜索寻找妖怪和尚过河问题的所有方式
算法
夜晚中的人海7 小时前
【C语言】------ 实现扫雷游戏
android·c语言·游戏
面会菜.7 小时前
C语言(队列)
c语言·开发语言
开心比对错重要7 小时前
leetcode69.x 的平方根
数据结构·算法·leetcode