【二叉树】Leetcode 117. 填充每个节点的下一个右侧节点指针 II【中等】

填充每个节点的下一个右侧节点指针 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 指针连接),‘#’ 表示每层的末尾。

解题思路

二叉树层序遍历中,把每层的元素通过next指针连接即可

  • 1、使用队列进行层次遍历。初始时将根节点加入队列。
  • 2、每次处理队列中的所有节点,设置当前层节点的 next 指针:
  • 遍历当前层的节点时,将当前节点的 next 指针指向队列中的下一个节点(同一层的下一个节点)。
  • 每处理完一个节点后,将其左右子节点加入队列,以便在下一层处理中使用。
  • 3、继续上述过程直到队列为空。

Java实现

public class ConnectRightNode {
    static class Node {
        int val;
        Node left;
        Node right;
        Node next;

        Node(int x) {
            val = x;
        }
    }

    public Node connect(Node root) {
        if (root == null) {
            return null;
        }

        Queue<Node> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            Node prev = null;
            for (int i = 0; i < size; i++) {
                Node current = queue.poll();
                if (prev != null) {
                    prev.next = current;
                }
                prev = current;

                if (current.left != null) {
                    queue.add(current.left);
                }
                if (current.right != null) {
                    queue.add(current.right);
                }
            }
        }

        return root;
    }

    public static void main(String[] args) {
        ConnectRightNode connectRightNode = new ConnectRightNode();

        // 示例二叉树
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.right = new Node(7);

        // 填充 next 指针
        connectRightNode.connect(root);

        // 打印结果,检查 next 指针是否正确
        printTree(root);
    }

    // 辅助方法:打印二叉树,显示 next 指针
    public static void printTree(Node root) {
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Node current = queue.poll();
                System.out.print(current.val + "(" + (current.next != null ? current.next.val : "#") + ") ");
                if (current.left != null) {
                    queue.add(current.left);
                }
                if (current.right != null) {
                    queue.add(current.right);
                }
            }
            System.out.println();
        }
    }
}

时间空间复杂度

  • 时间复杂度:O(n),其中 n 是二叉树中的节点数。需要遍历每个节点一次。
  • 空间复杂度:O(n),用于队列的空间。最坏情况下队列中包含二叉树中一整层的节点
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值