Leetcode 116. Populating Next Right Pointers in Each Node--完全二叉树层次遍历后放入队列链表--Queue,LinkedList

Posted 二十六画生的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 116. Populating Next Right Pointers in Each Node--完全二叉树层次遍历后放入队列链表--Queue,LinkedList相关的知识,希望对你有一定的参考价值。

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

struct Node 
  int val;
  Node *left;
  Node *right;
  Node *next;

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Example 1:

Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.

Example 2:

Input: root = []
Output: []

Constraints:

  • The number of nodes in the tree is in the range [0, 212 - 1].
  • -1000 <= Node.val <= 1000

Follow-up:

  • You may only use constant extra space.
  • The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.

Queue 、Deque:

add():从尾部插入

poll():从头部获取并删除

peek():从头部获取但不删除

package com.linkedlist;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author you guess
 * @Date 2022/4/5 14:57
 * @Version 1.0
 * @Desc
 */


// 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;
    
;

public class Leetcode_116_PopulatingNextRightPointersinEachNode 


    /**
     * while中用for,上次队列中的size循环完后再回到while,并且每次设置pre为null
     * <p>
     * Runtime: 2 ms, faster than 57.48% of Java online submissions for Populating Next Right Pointers in Each Node.
     * Memory Usage: 47.9 MB, less than 27.76% of Java online submissions for Populating Next Right Pointers in Each Node.
     *
     * @param root
     * @return
     */
    public Node connect(Node root) 
        if (root != null) 
            Queue<Node> queue = new LinkedList<Node>();
            int size;
            Node current, pre;

            queue.offer(root);//Inserts the specified element into this queue
            while (!queue.isEmpty()) 
                size = queue.size();
                pre = null;
                for (int i = 0; i < size; i++) 
                    current = queue.poll();//poll():Retrieves and removes the head of this queue,
                    if (pre != null) 
                        pre.next = current;
                    

                    pre = current;

                    if (current.left != null)
                        queue.offer(current.left);

                    if (current.right != null)
                        queue.offer(current.right);
                //for
            //while
        
        return root;
    


    /**
     * Wrong Answer
     * Input
     * [1,2,3,4,5,6,7]
     * Output
     * [1,2,3,4,5,6,7,#]
     * Expected
     * [1,#,2,3,#,4,5,6,7,#]
     *
     * @param root
     * @return
     */
    public Node connectSelf1(Node root) 
        ArrayDeque<Node> queue = new ArrayDeque<>();
        queue.addLast(root);
        Node temp = root;
        while (queue.size() > 0) 
            Node cur = queue.pollFirst();
            if (cur.left != null) queue.addLast(cur.left);
            if (cur.right != null) queue.addLast(cur.right);
            cur.left = null;
            cur.right = null;
            if (cur != root) 
                temp.next = cur;
                temp = cur;
            
        
        return root;
    


    /**
     * Wrong Answer
     * Input
     * [1,2,3,4,5,6,7]
     * Output
     * [1,2,3,4,5,6,7,#,2,3,4,5,6,7,#,4,5,6,7,#]
     * Expected
     * [1,#,2,3,#,4,5,6,7,#]
     *
     * @param root
     * @return
     */
    public Node connectSelf2(Node root) 
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        Node temp = root;
        while (queue.size() > 0) 
            Node cur = queue.poll();
            if (cur.left != null) queue.add(cur.left);
            if (cur.right != null) queue.add(cur.right);
            if (cur != root) 
                temp.next = cur;
                temp = cur;
            
        
        return root;
    

    public static void main(String[] args) 
        Node node7 = new Node(7, null, null, null);
        Node node6 = new Node(6, null, null, null);
        Node node5 = new Node(5, null, null, null);
        Node node4 = new Node(4, null, null, null);
        Node node3 = new Node(3, node6, node7, null);
        Node node2 = new Node(2, node4, node5, null);
        Node node1 = new Node(1, node2, node3, null);

        Leetcode_116_PopulatingNextRightPointersinEachNode main = new Leetcode_116_PopulatingNextRightPointersinEachNode();
        main.connect(node1);
    

以上是关于Leetcode 116. Populating Next Right Pointers in Each Node--完全二叉树层次遍历后放入队列链表--Queue,LinkedList的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-116-Populating Next Right Pointers in Each Node

leetcode 116. Populating Next Right Pointers in Each Node

leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

leetcode 116 Populating Next Right Pointers in Each Node ----- java

LeetCode OJ 116. Populating Next Right Pointers in Each Node

Leetcode 116. Populating next right pointers in each node I and II