zoukankan      html  css  js  c++  java
  • [LeetCode] 116. Populating Next Right Pointers in Each Node

    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.

    填充每个节点的下一个右侧节点指针。

    题意是给一个完美二叉树,请你按照图示补足所有的 next 指针。

    我这里给出两种思路,首先是BFS层序遍历。因为是一个完美二叉树所以可以这样做。版本二的时候这个方法则不行了。注意这道题在创建 queue 的时候,interface 用了 LinkedList 而非 Queue。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public Node connect(Node root) {
     3         // corner case
     4         if (root == null) {
     5             return root;
     6         }
     7 
     8         // normal case
     9         LinkedList<Node> queue = new LinkedList<>();
    10         queue.add(root);
    11         while (!queue.isEmpty()) {
    12             int size = queue.size();
    13             Node temp = queue.get(0);
    14             for (int i = 1; i < size; i++) {
    15                 temp.next = queue.get(i);
    16                 temp = queue.get(i);
    17             }
    18             for (int i = 0; i < size; i++) {
    19                 temp = queue.remove();
    20                 if (temp.left != null) {
    21                     queue.add(temp.left);
    22                 }
    23                 if (temp.right != null) {
    24                     queue.add(temp.right);
    25                 }
    26             }
    27         }
    28         return root;
    29     }
    30 }

    题目的 followup 要求不使用额外空间,所以另外一种做法的思路如下

    • 从根节点开始遍历,如果当前节点没有左孩子,说明已经遍历到最下面一层,在处理完这一层的 next 节点后就可以退出了
    • 如果当前节点有左节点,则说明还没有到最下面一层,此时当前节点的左孩子的 next 指针是右孩子(如果有的话)。比如例子中连接 2 和 3 的动作
    • 如果当前节点有 next 节点,说明当前节点不是根节点,或者当前节点不是当前层最右边的节点。此时还需要将当前节点的右孩子的 next 指针指向当前节点的 next 节点的左孩子(有点绕),并且在处理完这一步之后,移动到当前节点的 next 节点去继续处理。比如例子中连接 5 和 7 的动作
    • 当前层处理完之后,往左孩子走,去处理下一层节点

    时间O(n)

    空间O(1)

    Java实现

     1 class Solution {
     2     public Node connect(Node root) {
     3         // corner case
     4         if (root == null) {
     5             return root;
     6         }
     7 
     8         // normal case
     9         Node pre = root;
    10         while (pre.left != null) {
    11             Node temp = pre;
    12             while (temp != null) {
    13                 temp.left.next = temp.right;
    14                 // 如果temp不是root那一层,是可以往他的next节点移动的
    15                 if (temp.next != null) {
    16                     temp.right.next = temp.next.left;
    17                 }
    18                 temp = temp.next;
    19             }
    20             pre = pre.left;
    21         }
    22         return root;
    23     }
    24 }

    相关题目

    116. Populating Next Right Pointers in Each Node

    117. Populating Next Right Pointers in Each Node II

    199. Binary Tree Right Side View

    LeetCode 题目总结

  • 相关阅读:
    POJ 1681 Painter's Problem(高斯消元法)
    HDU 3530 Subsequence(单调队列)
    HDU 4302 Holedox Eating(优先队列或者线段树)
    POJ 2947 Widget Factory(高斯消元法,解模线性方程组)
    HDU 3635 Dragon Balls(并查集)
    HDU 4301 Divide Chocolate(找规律,DP)
    POJ 1753 Flip Game(高斯消元)
    POJ 3185 The Water Bowls(高斯消元)
    克琳:http://liyu.eu5.org
    WinDbg使用
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13286953.html
Copyright © 2011-2022 走看看