1. 程式人生 > >[LeetCode] 116. Populating Next Right Pointers in Each Node

[LeetCode] 116. Populating Next Right Pointers in Each Node

題目

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. Recursive approach is fine, implicit stack space does not count as extra space for this problem. You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). 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

思路

題目大意

給定一個滿二叉樹,每一層上的結點tNode,若它的右邊仍存在結點nNode,則該節點tNode.next = nNode。

解題思路

方法一 廣度遍歷

對樹進行廣度遍歷。在佇列的一層結點中,一個結點的後一個結點便是 結點的next;若當前結點為該層的最後一個結點,那麼該結點的next為null。

方法二 利用next指標

從高層到低層解決。 若一層結點的 next已經生成,那麼下一層的next可以直接生成。 starti為一層的第一個節點 ,若starti.left 不為空,cur = starti。

  1. 對於已生成next的某結點cur,若cur不為空 。其左子結點的next 指向其右子結點。
  2. 若cur 的next存在的話,cur 右子結點的next 為 starti.next.left.
  3. 將cur = cur.next ,跳轉 1。 1~3實現了starti下一層中從左到右結點next的建立。 starti = starti.left,starti跳轉到下一層。

code

方法一 廣度遍歷

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        if(root == null)    return;
            
        Queue<TreeLinkNode> queue = new LinkedList<>();
        queue.offer(root);
        
        
        while(queue.size() != 0){
            int nums = queue.size();

            for(int i = 1;i<nums;i++){
                TreeLinkNode tNode = queue.poll();
                tNode.next = queue.peek();
                if(tNode.left != null)  queue.offer(tNode.left);
                if(tNode.right != null)     queue.offer(tNode.right);   
            }
            TreeLinkNode tNode = queue.poll();
            tNode.next = null;
            if(tNode.left != null)  queue.offer(tNode.left);
            if(tNode.right != null)     queue.offer(tNode.right);            
        }
    }
}

方法二 利用next指標

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        if(root == null)    return;
        TreeLinkNode starti = root;
        TreeLinkNode cur;
        while(starti.left != null){
            cur = starti;
            while(cur != null){
                cur.left.next = cur.right;
                if(cur.next != null) cur.right.next = cur.next.left;
                cur = cur.next;
            }
            starti = starti.left;
        }
    }
}