zoukankan      html  css  js  c++  java
  • Populating Next Right Pointers in Each Node 解答

    Question

    Given a binary tree

        struct TreeLinkNode {
          TreeLinkNode *left;
          TreeLinkNode *right;
          TreeLinkNode *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.

    Note:

    • You may only use constant extra space.
    • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). 

    For example,
    Given the following perfect binary tree,

             1
           /  
          2    3
         /   / 
        4  5  6  7

    After calling your function, the tree should look like:

             1 -> NULL
           /  
          2 -> 3 -> NULL
         /   / 
        4->5->6->7 -> NULL

    Solution 1 -- BFS

    Key to the solution is to traverse tree level by level. Therefore, we can use BFS. Time complexity O(n), n is the number of nodes, and space cost is O(n).

     1 /**
     2  * Definition for binary tree with next pointer.
     3  * public class TreeLinkNode {
     4  *     int val;
     5  *     TreeLinkNode left, right, next;
     6  *     TreeLinkNode(int x) { val = x; }
     7  * }
     8  */
     9 public class Solution {
    10     public void connect(TreeLinkNode root) {
    11         // We can use BFS to solve this problem
    12         List<TreeLinkNode> current = new ArrayList<TreeLinkNode>();
    13         List<TreeLinkNode> next;
    14         if (root == null)
    15             return;
    16         current.add(root);
    17         while (current.size() > 0) {
    18             next = new ArrayList<TreeLinkNode>();
    19             int length = current.size();
    20             for (int i = 0; i < length; i++) {
    21                 TreeLinkNode currentTreeNode = current.get(i);
    22                 if (i < length - 1)
    23                     currentTreeNode.next = current.get(i + 1);
    24                 else
    25                     currentTreeNode.next = null;
    26                 if (currentTreeNode.left != null)
    27                     next.add(currentTreeNode.left);
    28                 if (currentTreeNode.right != null)
    29                     next.add(currentTreeNode.right);
    30             }
    31             current = next;
    32         }
    33     }
    34 }

    Solution 2 -- Recursive

    Consider for one node, the connection is done when:

    1. Its left node (if exists) connect to its right node.

    2. Its right node (if exists) connect to its next node's left child.

    3. Its left child and right child are all connected.

    Note the prerequisite for this problem is perfect binary tree (every parent has two children). Time complexity O(n), space cost O(1).

     1 /**
     2  * Definition for binary tree with next pointer.
     3  * public class TreeLinkNode {
     4  *     int val;
     5  *     TreeLinkNode left, right, next;
     6  *     TreeLinkNode(int x) { val = x; }
     7  * }
     8  */
     9 public class Solution {
    10     public void connect(TreeLinkNode root) {
    11         if (root == null)
    12             return;
    13         if (root.left != null)
    14             root.left.next = root.right;
    15         if (root.right != null && root.next != null)
    16             root.right.next = root.next.left;
    17         connect(root.left);
    18         connect(root.right);
    19     }
    20 }

    Solution 3 -- Four Pointers

    We can use four pointers to traverse the tree.

    (referrence: ProgramCreek)

     1 /**
     2  * Definition for binary tree with next pointer.
     3  * public class TreeLinkNode {
     4  *     int val;
     5  *     TreeLinkNode left, right, next;
     6  *     TreeLinkNode(int x) { val = x; }
     7  * }
     8  */
     9 public class Solution {
    10     public void connect(TreeLinkNode root) {
    11         if (root == null)
    12             return;
    13         TreeLinkNode lastHead = root;//prevous level's head 
    14         TreeLinkNode lastCurrent = null;//previous level's pointer
    15         TreeLinkNode currentHead = null;//current level's head
    16         TreeLinkNode current = null;//current level's pointer
    17         
    18         while (lastHead != null) {
    19             lastCurrent = lastHead;
    20             currentHead = lastHead.left;
    21             current = lastCurrent.left;
    22             while (lastCurrent != null && current != null) {
    23                 current.next = lastCurrent.right;
    24                 current = current.next;
    25                 lastCurrent = lastCurrent.next;
    26                 if (lastCurrent != null) {
    27                     current.next = lastCurrent.left;
    28                     current = current.next;
    29                 }
    30             }
    31             lastHead = currentHead;
    32             currentHead = null;
    33         }
    34     }
    35 }
  • 相关阅读:
    RabbitMQ死信队列另类用法之复合死信
    Asp.NetCore轻松学-使用Docker进行容器化托管
    Asp.NetCore轻松学-使用Supervisor进行托管部署
    Asp.NetCore轻松学-部署到 Linux 进行托管
    Asp.NetCore轻松学-部署到 IIS 进行托管
    一个提问引发的职业规划热议-拨开迷雾,走向光明
    Asp.Net Core 轻松学-使用MariaDB/MySql/PostgreSQL和支持多个上下文对象
    Asp.Net Core 轻松学-经常使用异步的你,可能需要看看这个文章
    Asp.Net Core 轻松学-10分钟使用EFCore连接MSSQL数据库
    Asp.Net Core 轻松学-多线程之Task(补充)
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4845698.html
Copyright © 2011-2022 走看看