zoukankan      html  css  js  c++  java
  • LeetCode 117. 填充每个节点的下一个右侧节点指针 II

    题目链接

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

    题目分析

    这个题的普通版本其实比较好实现,利用层序遍历去保存当前层的所有节点然后再使用next指针重建链表即可。
    但是有个进阶要求:使用常数级别的额外空间,这里就比较的难受了。
    其实再看回题目,根节点这层的链表肯定只有一个节点,那么我们可以在第i层把第i+1层的链表先建好,然后递归到i+1层的时候可以直接拿已经建好的链表去操作i+2层。
    但是要注意几种情况:

    1. 当前节点左右孩子都有,那么左孩子的next指向右孩子,右孩子的next指向哪?
    2. 只有左孩子。
    3. 只有右孩子。

    考虑到我们当前可以利用链表的机制寻找同一层的节点,那么我们可以遍历链表寻找下一个离当前孩子最近的节点,然后让当前孩子的next指向它。
    所谓的最近节点:

    1. 在链表中向右寻找同层节点
    2. 如果左孩子不为空,返回左孩子
    3. 右孩子不为空,返回右孩子
    4. 都为空的话一直向右遍历,直到遇到null

    当我们把当前的节点的孩子建立完链表后,我们要考虑如何去递归。
    如果我们先递归左孩子,那么在递归过程中有可能右孩子的链表还没建立起来,导致无法在下一层链表中寻找到下一个离当前孩子最近的节点。
    所以我们应该先递归右孩子,再递归左孩子。

    代码实现

    实现1

    class Solution {
        public Node connect(Node root) {
            if(root == null){
                return root;
            }
            Queue<Node> queue = new LinkedList<>();
            queue.offer(root);
            while(!queue.isEmpty()){
                int length = queue.size();
                Node pre = null;
                for(int i = 0; i < length; i++){
                    Node cur = queue.poll();
                    if(pre != null){
                        pre.next = cur;
                    }
                    pre = cur;
                    if(cur.left != null){
                        queue.offer(cur.left);
                    }
                    if(cur.right != null){
                        queue.offer(cur.right);
                    }
                }
            }
            return root;
        }
    }
    

    实现2

    /**
     * 补充节点的右侧指针,不是完美二叉树
     */
    public class Solution {
        public Node connect(Node root) {
            if (root == null || (root.right == null && root.left == null)) {
                return root;
            }
            if (root.left != null && root.right != null) {
                root.left.next = root.right;
                root.right.next = getNextNoNullChild(root);
            }
            if (root.left == null) {
                root.right.next = getNextNoNullChild(root);
            }
            if (root.right == null) {
                root.left.next = getNextNoNullChild(root);
            }
    
            //这里要注意:先递归右子树,否则右子树根节点next关系没建立好,左子树到右子树子节点无法正确挂载
            root.right = connect(root.right);
            root.left = connect(root.left);
    
            return root;
        }
    
        /**
         * 一路向右找到有子节点的根节点
         */
        private static Node getNextNoNullChild(Node root) {
            while (root.next != null) {
                if (root.next.left != null) {
                    return root.next.left;
                }
                if (root.next.right != null) {
                    return root.next.right;
                }
                root = root.next;
            }
            return null;
        }
    }
    
  • 相关阅读:
    拖动内容,滚动条滚动,横向
    前端面试题及答案整理(一)
    微软拼音转换工具类ChnCharInfo.dll
    table表格,让thead固定,tbody有滚动条,关键是都对齐的纯css写法。
    [C#]_[使用微软OpenXmlSDK (OpenXmlReader)读取xlsx表格] 读取大数据量100万条数据Excel文件解决方案
    asp.net mvc Post上传文件大小限制
    .net OADate 转javascript的Datetime js 5位 日期 转换
    在.NET开发中的单元测试工具之(1)——NUnit
    在.NET开发中的单元测试工具之(2)——xUnit.Net
    Git 头像修改 原
  • 原文地址:https://www.cnblogs.com/ZJPaang/p/13743171.html
Copyright © 2011-2022 走看看