【每日一题Day367】LC117填充每个节点的下一个右侧节点指针II | BFS

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

给定一个二叉树:

复制代码
struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

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

与LC116的区别在于,这个二叉树可能不是完全二叉树

  • BFS:使用队列进行BFS

    java 复制代码
    /*
    // 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;
            //cur我们可以把它看做是每一层的链表
            Node cur = root;
            while (cur != null) {
                //遍历当前层的时候,为了方便操作在下一
                //层前面添加一个哑结点(注意这里是访问
                //当前层的节点,然后把下一层的节点串起来)
                Node dummy = new Node(0);
                //pre表示访下一层节点的前一个节点
                Node pre = dummy;
                //然后开始遍历当前层的链表
                while (cur != null) {
                    if (cur.left != null) {
                        //如果当前节点的左子节点不为空,就让pre节点
                        //的next指向他,也就是把它串起来
                        pre.next = cur.left;
                        //然后再更新pre
                        pre = pre.next;
                    }
                    //同理参照左子树
                    if (cur.right != null) {
                        pre.next = cur.right;
                        pre = pre.next;
                    }
                    //继续访问这样行的下一个节点
                    cur = cur.next;
                }
                //把下一层串联成一个链表之后,让他赋值给cur,
                //后续继续循环,直到cur为空为止
                cur = dummy.next;
            }
            return root;
        }
    }
  • 迭代遍历

    1. 设置节点cur,遍历每一层的链表
    2. 设置节点pre,将下一层的节点串起来
    • 时间复杂度:O(n)
    • 空间复杂度:O(1)
    java 复制代码
    /*
    // 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;
            //cur我们可以把它看做是每一层的链表
            Node cur = root;
            while (cur != null) {
                //遍历当前层的时候,为了方便操作在下一
                //层前面添加一个哑结点(注意这里是访问
                //当前层的节点,然后把下一层的节点串起来)
                Node dummy = new Node(0);
                //pre表示访下一层节点的前一个节点
                Node pre = dummy;
                //然后开始遍历当前层的链表
                while (cur != null) {
                    if (cur.left != null) {
                        //如果当前节点的左子节点不为空,就让pre节点
                        //的next指向他,也就是把它串起来
                        pre.next = cur.left;
                        //然后再更新pre
                        pre = pre.next;
                    }
                    //同理参照左子树
                    if (cur.right != null) {
                        pre.next = cur.right;
                        pre = pre.next;
                    }
                    //继续访问这样行的下一个节点
                    cur = cur.next;
                }
                //把下一层串联成一个链表之后,让他赋值给cur,
                //后续继续循环,直到cur为空为止
                cur = dummy.next;
            }
            return root;
        }
    }
相关推荐
强德亨上校24 分钟前
贪心算法(Greedy Algorithm)详解
算法·贪心算法
浮灯Foden1 小时前
算法-每日一题(DAY13)两数之和
开发语言·数据结构·c++·算法·leetcode·面试·散列表
西工程小巴2 小时前
实践笔记-VSCode与IDE同步问题解决指南;程序总是进入中断服务程序。
c语言·算法·嵌入式
Tina学编程2 小时前
48Days-Day19 | ISBN号,kotori和迷宫,矩阵最长递增路径
java·算法
Moonbit2 小时前
MoonBit Perals Vol.06: MoonBit 与 LLVM 共舞 (上):编译前端实现
后端·算法·编程语言
百度Geek说4 小时前
第一!百度智能云领跑视觉大模型赛道
算法
big_eleven4 小时前
轻松掌握数据结构:二叉树
后端·算法·面试
big_eleven4 小时前
轻松掌握数据结构:二叉查找树
后端·算法·面试
CoovallyAIHub4 小时前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·计算机视觉
kyle~5 小时前
OpenCV---特征检测算法(ORB,Oriented FAST and Rotated BRIEF)
人工智能·opencv·算法