116. Populating Next Right Pointers in Each Node

題目:node

Given a binary treeide

    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.spa

Note:code

  • 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,blog

         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

 

 

Hide Tags
  Tree Depth-first Search  

連接:  http://leetcode.com/problems/populating-next-right-pointers-in-each-node/ci

題解:leetcode

使用DFS。題目給出徹底二叉樹,因此只要先判斷next節點是否爲空,接下來斷定root的左右子節點是否爲空就能夠了。get

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
        TreeLinkNode node = root.next;
        
        if(node != null){
            if(node.right != null){
                root.right.next = node.left;    
                root.left.next = root.right;    
            }
        } else {
            if(root.right != null){
                root.right.next = null;    
                root.left.next = root.right;    
            }
       }
        
        connect(root.left);
        connect(root.right);
    }
}

Update:  

以前寫得好醜..精簡一下。遞歸的Space Complexity怎麼算? 這裏創建了一個TreeLinkNode,是reference type,這個object的reference存在stack裏,但object是存在heap裏。  還要學習JVM的許多知識。

Time Complexity - O(n), Space Complexity - O(1).

/**
 * 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 p = root.next;
        if(root.left != null)
            root.left.next = root.right;
        if(root.right != null)
            root.right.next = (p == null) ? null : p.left;
        connect(root.left);
        connect(root.right);
    }
}

 

二刷:

Java:

/**
 * 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 p = root.next;
        if (root.left != null) {
            root.left.next = root.right;
            root.right.next = p == null ? null : p.left; 
        }
        connect(root.left);
        connect(root.right);
    }
}

 

三刷:

跟以前的方法同樣使用了recursive。但其實iterative的solution更好。

Java:

Recursive:

Time Complexity - O(n), Space Complexity - O(n)

/**
 * 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;
        if (root.right != null) {
            root.left.next = root.right;
            if (root.next != null) root.right.next = root.next.left;
        }
        connect(root.left);
        connect(root.right);
    }
}

  

Iterative:

解法來自 yavinci大神。

兩層循環,當root和root.left不爲空的時候,咱們要利用好本身建立的next節點來不斷向右進行遍歷。遍歷完這一層之後咱們能夠設置root = root.left,這樣咱們到了下一層,繼續向右進行遍歷。

Time Complexity - O(n), Space Complexity - O(1)

 

/**
 * 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) {
        while (root != null && root.left != null) {
            TreeLinkNode cur = root;
            while (cur != null) {
                cur.left.next = cur.right;
                if (cur.next != null) cur.right.next = cur.next.left;
                cur = cur.next;
            }
            root = root.left;
        }
    }
}

 

Update:

/**
 * 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) {
        TreeLinkNode curLevel = new TreeLinkNode(-1);
        TreeLinkNode newLevel = curLevel;
        while (root != null) {
            if (root.left != null) {
                curLevel.next = root.left;
                curLevel = curLevel.next;
            }
            if (root.right != null) {
                curLevel.next = root.right;
                curLevel = curLevel.next;
            }
            root = root.next;
            if (root == null) {
                curLevel = newLevel;
                root = newLevel.next;
                newLevel.next = null;
            }
        }
    }
}

 

 

 

 

 

Reference:

https://leetcode.com/discuss/7327/a-simple-accepted-solution

相關文章
相關標籤/搜索