数据结构与算法之字符串: LeetCode 117. 填充每个节点的下一个右侧节点指针 II (Ts版)

填充每个节点的下一个右侧节点指针 II

描述

  • 给定一个二叉树:

    struct Node {
      int val;
      Node *left;
      Node *right;
      Node *next;
    }
    
  • 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL 。

  • 初始状态下,所有 next 指针都被设置为 NULL 。

示例 1

输入:root = [1,2,3,4,5,null,7]
输出:[1,#,2,3,#,4,5,7,#]

解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化输出按层序遍历顺序(由 next 指针连接),‘#’ 表示每层的末尾。

示例 2

输入:root = []
输出:[]

提示

  • 树中的节点数在范围 [0, 6000] 内
  • -100 <= Node.val <= 100

进阶

  • 你只能使用常量级额外空间
  • 使用递归解题也符合要求,本题中递归程序的隐式栈空间不计入额外空间复杂度

Typescript 版算法实现


1 ) 方案1:层次遍历

/**
 * Definition for _Node.
 * class _Node {
 *     val: number
 *     left: _Node | null
 *     right: _Node | null
 *     next: _Node | null
 * 
 *     constructor(val?: number, left?: _Node, right?: _Node, next?: _Node) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function connect(root: _Node | null): _Node | null {
    if (root === null) {
        return null;
    }
    const queue = [root];
    while (queue.length) {
        const n = queue.length;
        let last = null;
        for (let i = 1; i <= n; ++i) {
            let f = queue.shift();
            if (f.left !== null) {
                queue.push(f.left);
            }
            if (f.right !== null) {
                queue.push(f.right);
            }
            if (i !== 1) {
                last.next = f;
            }
            last = f;
        }
    }
    return root;
};

2 ) 方案2:使用已建立的 next 指针

/**
 * Definition for _Node.
 * class _Node {
 *     val: number
 *     left: _Node | null
 *     right: _Node | null
 *     next: _Node | null
 * 
 *     constructor(val?: number, left?: _Node, right?: _Node, next?: _Node) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function connect(root: TreeNode | null): TreeNode | null {
    if (!root) return null;

    const handle = (p: TreeNode, last: TreeNode | null, nextStart: TreeNode | null): [TreeNode | null, TreeNode | null] => {
        if (last) {
            last.next = p;
        }
        if (!nextStart) {
            nextStart = p;
        }
        return [p, nextStart];
    };

    let start = root;
    while (start) {
        let last: TreeNode | null = null;
        let nextStart: TreeNode | null = null;

        for (let p = start; p; p = p.next) {
            if (p.left) {
                [last, nextStart] = handle(p.left, last, nextStart);
            }
            if (p.right) {
                [last, nextStart] = handle(p.right, last, nextStart);
            }
        }
        start = nextStart;
    }

    return root;
}

// 定义二叉树节点的接口
interface TreeNode {
    val: number;
    left: TreeNode | null;
    right: TreeNode | null;
    next: TreeNode | null;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wang's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值