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

文章介绍了如何通过层序遍历的方式填充二叉树的next指针,使其指向下一个右侧节点,同时处理了空树和边界条件,使用常量级额外空间实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


117. 填充每个节点的下一个右侧节点指针 II
难度: 中等
来源: 每日一题 2023.11.03

给定一个二叉树:

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

进阶:

你只能使用常量级额外空间。

使用递归解题也符合要求,本题中递归程序的隐式栈空间不计入额外空间复杂度。

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}
    
    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/

class Solution {
    public Node connect(Node root) {

    }
}

分析与题解

  • 层序遍历

    这个题目实际上就是层序遍历的变形题, 我们在层序遍历的过程, 根据具体情况添加 next 节点即可.

    当然了, 有一些边界条件需要处理, 比如当前二叉树为空, 直接返回空即可.

    if(root == null) {
        return root;
    }
    

    再比如每一层的最后一个节点的 next节点 一定是空节点.

    if (i == length - 1) {
        node.next = null;
    }
    

    然后, 我们正常的进行层序遍历即可.

    while(!deque.isEmpty()) {
        int length = deque.size();
        Node preNode = null;
        for(int i = 0; i < length; i++) {
            Node node = deque.poll();
            if (i == length - 1) {
                node.next = null;
            }
            if (preNode != null) {
                preNode.next = node;
            }
            if (node.left != null) {deque.offer(node.left);} 
            if (node.right != null) {deque.offer(node.right);} 
            preNode = node;
        }
    }
    

    接下来, 我们就看一下整体的题解过程.

    /*
    // Definition for a Node.
    class Node {
        public int val;
        public Node left;
        public Node right;
        public Node next;
    
        public Node() {}
        
        public Node(int _val) {
            val = _val;
        }
    
        public Node(int _val, Node _left, Node _right, Node _next) {
            val = _val;
            left = _left;
            right = _right;
            next = _next;
        }
    };
    */
    
    class Solution {
        public Node connect(Node root) {
            // 二叉树的层序遍历的变形题
            if(root == null) {
                return root;
            }
            Deque<Node> deque = new ArrayDeque<>();
            deque.offer(root);
            while(!deque.isEmpty()) {
                int length = deque.size();
                Node preNode = null;
                for(int i = 0; i < length; i++) {
                    Node node = deque.poll();
                    if (i == length - 1) {
                        node.next = null;
                    }
                    if (preNode != null) {
                        preNode.next = node;
                    }
                    if (node.left != null) {deque.offer(node.left);} 
                    if (node.right != null) {deque.offer(node.right);} 
                    preNode = node;
                }
            }
            return root;
        }
    }
    

    复杂度分析:

    • 时间复杂度: O(n), 循环遍历的时间复杂度与二叉树的节点成线性关系
    • 空间复杂度: O(n), 队列所需要的时间复杂度与二叉树的节点成线性关系

    结果如下所示.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

神经骚栋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值