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;
}
相关推荐
szuzhan.gy25 分钟前
DS查找—二叉树平衡因子
数据结构·c++·算法
一只码代码的章鱼1 小时前
排序算法 (插入,选择,冒泡,希尔,快速,归并,堆排序)
数据结构·算法·排序算法
青い月の魔女1 小时前
数据结构初阶---二叉树
c语言·数据结构·笔记·学习·算法
最后一个bug2 小时前
STM32MP1linux根文件系统目录作用
linux·c语言·arm开发·单片机·嵌入式硬件
林的快手2 小时前
209.长度最小的子数组
java·数据结构·数据库·python·算法·leetcode
FeboReigns2 小时前
C++简明教程(4)(Hello World)
c语言·c++
FeboReigns2 小时前
C++简明教程(10)(初识类)
c语言·开发语言·c++
千天夜2 小时前
多源多点路径规划:基于启发式动态生成树算法的实现
算法·机器学习·动态规划
从以前2 小时前
准备考试:解决大学入学考试问题
数据结构·python·算法
.Vcoistnt3 小时前
Codeforces Round 994 (Div. 2)(A-D)
数据结构·c++·算法·贪心算法·动态规划