• 【LeetCode】117.填充每个节点的下一个右侧节点指针 II(优雅解法,看完跪)


    在这里插入图片描述

    分析

    这道题和116题基本一样,区别是之前是满二叉树,而这个不是。

    解法一 BFS

    直接将116题的代码复制过来就好,一句也不用改。详细可以去我的博客116题看看。

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

    优雅解法

    利用解法一的思想,我们利用 pre 指针,然后一个一个取节点,把它连起来。因为我们没有添加为 null 的节点,就是下边的代码的作用。所以我们不用考虑当前节点为null的情况。

    if (cur.left != null) {
        queue.offer(cur.left);
    }
    if (cur.right != null) {
        queue.offer(cur.right);
    }
    
    

    所以这里是一样的,如果当前节点为null不处理就可以了。

    第二个问题,怎么得到每次的开头的节点呢?我们用一个dummy指针,当连接第一个节点的时候,就将dummy指针指向他。此外,之前用的pre指针,把它当成tail指针可能会更好理解。如下图所示:
    在这里插入图片描述

    cur 指针利用 next 不停的遍历当前层。

    如果 cur 的孩子不为 null 就将它接到 tail后边,然后更新tail

    curnull的时候,再利用 dummy指针得到新的一层的开始节点。

    dummy指针在链表中经常用到,他只是为了处理头结点的情况,它并不属于当前链表。
    代码就异常的简单了。

    Node connect(Node root) {
        Node cur = root;
        while (cur != null) {
            Node dummy = new Node();
            Node tail = dummy;
            //遍历 cur 的当前层
            while (cur != null) {
                if (cur.left != null) {
                    tail.next = cur.left;
                    tail = tail.next;
                }
                if (cur.right != null) {
                    tail.next = cur.right;
                    tail = tail.next;
                }
                cur = cur.next;
            }
            //更新 cur 到下一层
            cur = dummy.next;
        }
        return root;
    }
    
    

    递归

    核心是getNextNoNullChild,根据root,找到下一级右手第一个
    然后分情况讨论,对每个节点:
    左子右子都有,则左子next指向右子,右子next指向getNextNoNullChild
    只有左子,左子指向getNextNoNullChild
    只有右子,右子指向getNextNoNullChild

    注意:递归时要先递归右子树,否则上级节点next关系没建好,下级无法成功getNextNoNullChild

    // package com.leetcode.explore.learnCard.dataStructureBinaryTree.conclusion4;
    
    /**
     * 补充节点的右侧指针,不是完美二叉树
     */
    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;
        }
    }
    
    
  • 相关阅读:
    python_接口基础知识
    python_基础总结
    python_配置文件_yaml
    python_loggin日志处理
    python_数据驱动_ddt
    python_unittest_单元测试_openpyxl
    python_类与对象总结_继承
    python_路径操作及类和对象
    python_导包
    Codeforces Round #655 (Div. 2) B. Omkar and Last Class of Math
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13308074.html
Copyright © 2020-2023  润新知