美文网首页
2021-02-22 117. 填充每个节点的下一个右侧节点指针

2021-02-22 117. 填充每个节点的下一个右侧节点指针

作者: 止戈学习笔记 | 来源:发表于2021-02-21 15:22 被阅读0次

    题目地址

    https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/

    题目描述

    给定一个二叉树
    struct Node {
      int val;
      Node *left;
      Node *right;
      Node *next;
    }
    填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
    初始状态下,所有 next 指针都被设置为 NULL。
    
    进阶:
    你只能使用常量级额外空间。
    使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
     
    示例:
    输入:root = [1,2,3,4,5,null,7]
    输出:[1,#,2,3,#,4,5,7,#]
    解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。
     
    提示:
    树中的节点数小于 6000
    -100 <= node.val <= 100
    

    思路

    1. 借助栈层次遍历来连接next, 每一层遍历完都有个子循环遍历下一层(同时连接next), root遍历完, 遍历下一层, root.left和root.right, 同时连接next, 这时下一层也都添加进queue, 接着遍历下一层.
    2. 递归连接, 先将右边子树连接好, 否则某一层的子树中间间隔很多空的就是就会误认为中间的子树是最右侧子树. 因为上一层和右边子树都是连接好的, 所以next都可以直接通过root.next.next.....找到.

    题解

    /**
     * Created by zcdeng on 2021/2/22.
     */
    public class TreeConnectV2 {
        public static void main(String[] args) {
            Node root = Node.create(new int[] {1, 2, 3, 4, 5, -1, 7});
            Node ret = connect(root);
    
            root = Node.create(new int[] {1, 2, 3, 4, 5});
            ret = connectV0(root);
        }
        /**
         * 执行用时:3 ms, 在所有 Java 提交中击败了8.80%的用户
         * 内存消耗:38.3 MB, 在所有 Java 提交中击败了12.48%的用户
         */
        private static Node connectV0(Node root) {
            if (root == null) {
                return null;
            }
            List<Node> queue = new ArrayList<Node>();
            queue.add(root);
            while (queue.size() > 0) {
                int size = queue.size();
                while (size > 0) {
                    size--;
                    Node node = queue.remove(0);
                    if (size > 0 && queue.size() > 0) {
                        node.next = queue.get(0);
                    }
    
                    if (node.left != null) {
                        queue.add(node.left);
                    }
    
                    if (node.right != null) {
                        queue.add(node.right);
                    }
                }
            }
            return root;
        }
        /**
         * 执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
         * 内存消耗:38.4 MB, 在所有 Java 提交中击败了7.56%的用户
         */
        private static Node connect(Node root) {
            if (root == null) {
                return root;
            }
            if (root.left != null) {
                if (root.right != null) {
                    root.left.next = root.right;
                } else {
                    root.left.next = getNext(root.next);
                }
            }
    
            if (root.right != null) {
                root.right.next = getNext(root.next);
            }
    
            connect(root.left);
            connect(root.right);
            return root;
        }
        private static Node getNext(Node next) {
            while (next != null) {
                if (next.left != null) {
                    return next.left;
                }
                if (next.right != null) {
                    return next.right;
                }
                next = next.next;
            }
            return null;
        }
    }
    

    相关文章

      网友评论

          本文标题:2021-02-22 117. 填充每个节点的下一个右侧节点指针

          本文链接:https://www.haomeiwen.com/subject/puahfltx.html