zoukankan      html  css  js  c++  java
  • LeetCode: Populating Next Right Pointers in Each Node 解题报告

    Populating Next Right Pointers in Each Node Total
    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

    我们可以用递归处理左右子树. 这个算法可能会消耗O(h)的栈空间。

     1 /* 试一下 recursion */
     2     public void connect(TreeLinkNode root) {
     3         if (root == null) {
     4             return;
     5         }
     6         
     7         rec(root);
     8     }
     9 
    10     public void rec(TreeLinkNode root) {
    11         if (root == null) {
    12             return;
    13         }
    14 
    15         if (root.left != null) {
    16             root.left.next = root.right;            
    17         }
    18 
    19         if (root.right != null) {
    20             root.right.next = root.next.left;
    21         }
    22         
    23         rec(root.left);
    24         rec(root.right);
    25     }

     2014.1229 redo:

     1 /*
     2         3. A recursion version.
     3     */
     4     public void connect3(TreeLinkNode root) {
     5         if (root == null || root.left == null) {
     6             return;
     7         }
     8         
     9         root.left.next = root.right;
    10         root.right.next = root.next == null ? null: root.next.left;
    11         
    12         connect(root.left);
    13         connect(root.right);
    14     }
    View Code

    SOLUTION 2

    用层次遍历也可以相当容易解出来,而且这种方法用在下一题一点不用变。

     但是 这个解法不能符合题意。题目要求我们使用 constant extra space.

     1 /*
     2      * 使用level traversal来做。
     3      * */
     4     public void connect1(TreeLinkNode root) {
     5         if (root == null) {
     6             return;
     7         }
     8         
     9         TreeLinkNode dummy = new TreeLinkNode(0);
    10         Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
    11         q.offer(root);
    12         q.offer(dummy);
    13         
    14         while (!q.isEmpty()) {
    15             TreeLinkNode cur = q.poll();
    16             if (cur == dummy) {
    17                 if (!q.isEmpty()) {
    18                     q.offer(dummy);
    19                 }
    20                 continue;
    21             }
    22             
    23             if (q.peek() == dummy) {
    24                 cur.next = null;
    25             } else {
    26                 cur.next = q.peek();
    27             }
    28             
    29             if (cur.left != null) {
    30                 q.offer(cur.left);
    31             }
    32             
    33             if (cur.right != null) {
    34                 q.offer(cur.right);
    35             }
    36         }
    37     }

     2014.1229 redo:

    1.  

     1 /*
     2         1. Iterator.
     3     */
     4     public void connect1(TreeLinkNode root) {
     5         if (root == null) {
     6             return;
     7         }
     8         
     9         Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
    10         q.offer(root);
    11         
    12         while (!q.isEmpty()) {
    13             int size = q.size();
    14             
    15             for (int i = 0; i < size; i++) {
    16                 TreeLinkNode cur = q.poll();
    17                 
    18                 // ERROR 2: forget to determine if root don't have left and right.
    19                 if (cur.left == null) {
    20                     return;
    21                 }
    22                 
    23                 cur.left.next = cur.right;
    24                 cur.right.next = cur.next == null ? null : cur.next.left;
    25                 // bug 1: should put the offer inside the for loop
    26                 q.offer(cur.left);
    27                 q.offer(cur.right);
    28             }
    29         }
    30     }
    View Code

    2. 

     1 /*
     2         2. Iterator. More simple version.
     3     */
     4     public void connect2(TreeLinkNode root) {
     5         if (root == null) {
     6             return;
     7         }
     8         
     9         Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
    10         q.offer(root);
    11         
    12         while (!q.isEmpty()) {
    13             int size = q.size();
    14             
    15             for (int i = 0; i < size; i++) {
    16                 TreeLinkNode cur = q.poll();
    17                 
    18                 // bug 1: should judge the size!
    19                 cur.next = (i == size - 1) ? null: q.peek();
    20                 
    21                 if (cur.left != null) {
    22                     q.offer(cur.left);
    23                     q.offer(cur.right);
    24                 }
    25             }
    26         }
    27     }
    View Code

    SOLUTION 3

    把层次遍历修改一下,就是下面的解法了,我们使用2个循环,一个指针P1专门记录每一层的最左边节点,另一个指针P2扫描本层,把下一层的链接上。

    下层链接完成后,将P1移动到它的左孩子即可。

    这个算法的空间复杂度是O(1). 没有额外的空间。

     1 /*
     2         The version that only has O(1) space complexity.
     3     */
     4     public void connect(TreeLinkNode root) {
     5         if (root == null) {
     6             return;
     7         }
     8         
     9         Iterator(root);
    10     }
    11     
    12     public void Iterator(TreeLinkNode root) {
    13         if (root == null) {
    14             return;
    15         }
    16         
    17         TreeLinkNode leftEnd = root;
    18         
    19         while(leftEnd != null) {
    20             // go through the current level and link the next level.
    21             TreeLinkNode cur = leftEnd;
    22             while (cur != null) {
    23                 if (cur.left == null) {
    24                     break;
    25                 }
    26                 
    27                 cur.left.next = cur.right;
    28                 // 一定要记得判null.
    29                 cur.right.next = cur.next == null ? null: cur.next.left;
    30                 
    31                 cur = cur.next;
    32             }
    33             
    34             // get to the next level.
    35             leftEnd = leftEnd.left;
    36         }
    37     }
    View Code

     2014.1229 redo:

     1 /*
     2         4. Another constant iterator version.
     3     */
     4     public void connect(TreeLinkNode root) {
     5         if (root == null) {
     6             return;
     7         }
     8         
     9         TreeLinkNode leftEnd = root;
    10         while (leftEnd != null && leftEnd.left != null) {
    11             TreeLinkNode cur = leftEnd;
    12             while (cur != null) {
    13                 cur.left.next = cur.right;
    14                 cur.right.next = cur.next == null ? null: cur.next.left;
    15                 
    16                 cur = cur.next;
    17             }
    18             
    19             leftEnd = leftEnd.left;
    20         }
    21     }
    View Code

    GITHUB:

    https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/Connect_2014_1229.java

  • 相关阅读:
    BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路
    BZOJ 1708: [Usaco2007 Oct]Money奶牛的硬币
    BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间
    BZOJ 1611: [Usaco2008 Feb]Meteor Shower流星雨
    BZOJ 1610: [Usaco2008 Feb]Line连线游戏
    BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
    BZOJ 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
    BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草
    BZOJ 1083: [SCOI2005]繁忙的都市
    STL set的用法
  • 原文地址:https://www.cnblogs.com/yuzhangcmu/p/4041341.html
Copyright © 2011-2022 走看看