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;
}
相关推荐
沐欣工作室_lvyiyi3 分钟前
基于单片机的跑步机控制系统设计(论文+源码)
c语言·单片机·嵌入式硬件·课程设计·keil·跑步机设计
苏辰澈8 分钟前
C++自己的族谱规则:继承详解下
c语言·开发语言·c++·visualstudio
她说彩礼65万17 分钟前
C语言 const的使用
java·服务器·c语言
圣保罗的大教堂44 分钟前
leetcode 3302. 字典序最小的合法序列 中等
leetcode
py有趣1 小时前
硬件SPI和软件模拟SPI在配置方面的一点区别
c语言·stm32·单片机
驭渊的小故事1 小时前
NC229023 题解:二分答案求解“最大化最小值“问题
java·算法
朝朝辞暮i1 小时前
C++ 第 36 课:Action——机械臂/VLA 非常重要
开发语言·c++·算法·ros2
Logic1011 小时前
C语言/数据结构位运算题解:异或XOR找出英雄队伍中的“独特战斗力“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
ra90fy1 小时前
B3873 [GESP202309 六级] 小杨买饮料
c++·算法
Omics Pro1 小时前
北理工李荣华:AI虚拟细胞证据多模态推理基准
数据库·人工智能·算法·机器学习·自然语言处理