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;
}
相关推荐
却话巴山夜雨时i1 分钟前
74. 搜索二维矩阵【中等】
数据结构·算法·矩阵
sin_hielo7 分钟前
leetcode 3512
数据结构·算法·leetcode
_F_y8 分钟前
二分:二分查找、在排序数组中查找元素的第一个和最后一个位置、搜索插入位置、x 的平方根
c++·算法
Elias不吃糖8 分钟前
LeetCode--130被围绕的区域
数据结构·c++·算法·leetcode·深度优先
烛衔溟9 分钟前
C语言算法:动态规划基础
c语言·算法·动态规划·算法设计·dp基础
sinat_6020353614 分钟前
翁恺 6-
c语言
誰能久伴不乏18 分钟前
进程通信与线程通信:全面总结 + 使用场景 + 优缺点 + 使用方法
linux·服务器·c语言·c++
im_AMBER22 分钟前
数据结构 12 图
数据结构·笔记·学习·算法·深度优先
STY_fish_20121 小时前
P11855 [CSP-J2022 山东] 部署
算法·图论·差分
myw0712051 小时前
湘大头歌程-Ride to Office练习笔记
c语言·数据结构·笔记·算法