爲二叉樹添加兄弟節點的指針(非徹底二叉樹)

Populating Next Right Pointers in Each Node II

問題:node

Follow up for problem "Populating Next Right Pointers in Each Node".spa

What if the given tree could be any binary tree? Would your previous solution still work?指針

Note:對象

  • You may only use constant extra space.

For example,
Given the following binary tree,遞歸

        1
       /  \
      2    3
     / \    \
    4   5    7

After calling your function, the tree should look like:it

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

解決:io

① 與Ⅰ不一樣的是二叉樹不必定是徹底二叉樹。在判斷下一個是否爲空時還要判斷父節點的下一節點的子節點的狀況。遞歸遍歷。ast

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {//1ms
    public void connect(TreeLinkNode root) {
        if(root == null) return;
        TreeLinkNode rootNext = root.next;
        TreeLinkNode next = null;
        // rootNext若是是null說明已經處理完這一層的全部node  
        // next不等於null說明找到了找到最左邊的下一個被鏈接的對象  
        while(rootNext != null && next == null){
            if(rootNext.left != null){// 優先找左邊  
                next = rootNext.left;
            }else{
                next = rootNext.right;
            }
            rootNext = rootNext.next;
        }
        if(root.left != null){
            if(root.right != null){// 內部相連  
                root.left.next = root.right;
            }else{ // 跨樹相連  
                root.left.next = next;
            }
        }
        if(root.right != null){ // 跨樹相連  
            root.right.next = next;
        }
        connect(root.right);// 要先讓右邊都先連起來  
        connect(root.left);
    }
}function

② 非遞歸方法。與I相同,不須要改變。class

public class Solution { //4ms
    public void connect(TreeLinkNode root) {
        if(root == null) return;
        Queue<TreeLinkNode> queue = new LinkedList<>();
        queue.offer(root);
        while(! queue.isEmpty()){
            int count = queue.size();
            for (int i = 0;i < count ;i ++ ) {
                TreeLinkNode cur = queue.poll();
                if(i < count - 1){
                    cur.next = queue.peek();
                }
                if(cur.left != null) queue.offer(cur.left);
                if(cur.right != null) queue.offer(cur.right);
            }
        }
    }
}

③ 空間複雜度爲O(1)的方法。首先定義了一個變量,用來指向每一層最左邊的一個節點,因爲左子結點可能缺失,因此這個最左邊的節點有多是上一層某個節點的右子結點,咱們每往下推一層時,還要有個指針指向上一層的父節點,由於因爲右子節點的可能缺失,因此上一層的父節點必須向右移,直到移到有子節點的父節點爲止,而後把next指針連上,而後當前層的指針cur繼續向右偏移,直到連完當前層全部的子節點,再向下一層推動,以此類推能夠連上全部的next指針。

class Solution{ //1ms     public void connect(TreeLinkNode root) {         if(root == null) return;         TreeLinkNode lastHead = root;//指向上一層的頭節點         TreeLinkNode lastCurrent = null;//上一層的指針         TreeLinkNode currentHead = null;//當前層的頭節點         TreeLinkNode current = null;//當前層的指針         while(lastHead != null){             lastCurrent = lastHead;              while(lastCurrent != null){                 //左子節點不爲空                 if(lastCurrent.left!=null)    {                     if(currentHead == null){                         currentHead = lastCurrent.left;                         current = lastCurrent.left;                     }else{                         current.next = lastCurrent.left;                         current = current.next;                     }                 }                 //右子節點不爲空                 if(lastCurrent.right != null){                     if(currentHead == null){                         currentHead = lastCurrent.right;                         current = lastCurrent.right;                     }else{                         current.next = lastCurrent.right;                         current = current.next;                     }                 }                 lastCurrent = lastCurrent.next;             }             //更新頭節點             lastHead = currentHead;             currentHead = null;         }     } }

相關文章
相關標籤/搜索