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 }
  • 相关阅读:
    2015年工作中遇到的问题:21-30(这10个问题很有价值)
    简要总结最近遇到的5个问题
    简要总结最近遇到的5个问题
    我到底要选择一种什么样的生活方式,度过这一辈子呢:人生自由与职业发展方向(下)
    我到底要选择一种什么样的生活方式,度过这一辈子呢:人生自由与职业发展方向(下)
    IT咨询服务-客户案例(四):根据图片等素材,动态生成个性化图片
    IT咨询服务-客户案例(四):根据图片等素材,动态生成个性化图片
    分布式系统若干经验总结
    分布式系统若干经验总结
    最近遇到的若干技术问题
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4845698.html
Copyright © 2011-2022 走看看