117. Populating Next Right Pointers in Each Node II

題目:html

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

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

Note:python

  • You may only use constant extra space.

 

For example,
Given the following binary tree,spring

         1
       /  \
      2    3
     / \    \
    4   5    7

 

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

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

 

 

Hide Tags
  Tree Depth-first Search  

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

題解:code

同樣是DFS。跟上題不一樣在於,給定輸入不是徹底二叉樹了,因此要加入一些條件來判斷是否存在一個合理的next值。而且對左節點和右節點的有效性也要驗證。最後要先遞歸鏈接右節點,再connect左節點。htm

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

public class Solution {
    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
        TreeLinkNode node = root.next;
        while(node != null){
            if(node.left != null){
                node = node.left;
                break;
            } else if(node.right != null){
                node = node.right;
                break;
            } 
            node = node.next;
        }
        
        if(root.right != null){
            root.right.next = node;
            if(root.left != null)
                root.left.next = root.right;
        } else {
            if(root.left != null)
                root.left.next = node;
        }
        
        connect(root.right);
        connect(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) {
        if(root == null)
            return;
        TreeLinkNode p = root.next;
        
        while(p != null) {
            if(p.left != null) {
                p = p.left;
                break;
            } else if (p.right != null) {
                p = p.right;
                break;
            }
            p = p.next;    
        }
        
        if(root.right != null) 
            root.right.next = p;
        if(root.left != null) 
            root.left.next = (root.right != null) ? root.right : p;
        
        connect(root.right);
        connect(root.left);
    }
}

題外話: 剛看完crimson peak,還不錯。不太小小失望是原本覺得是個恐怖片,觀衆小朋友們買好了可樂和爆米花準備挑戰一下本身,結果是個離奇曲折婉轉動人的悽美愛情片...我只想說,導演你太浪漫了, 我先刷兩題壓壓驚 -______-!!

 

二刷:

依然使用了遞歸,並無作到constant space。留給三刷了。

Java:

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;
        TreeLinkNode nextNode = root.next;
        while (nextNode != null) {
            if (nextNode.left == null && nextNode.right == null) nextNode = nextNode.next;
            else break;
        }
        if (nextNode != null) nextNode = (nextNode.left != null) ? nextNode.left : nextNode.right;
        if (root.right != null) root.right.next = nextNode;
        if (root.left != null) root.left.next = (root.right != null) ? root.right : nextNode;
        connect(root.right);
        connect(root.left);
    }
}

 

iterative:

Level order traversal。主要就是相似二叉樹層序遍歷。這回把頂層看做一個linkedlist,咱們只須要繼續鏈接這linkedlist中每一個節點的子節點們。當頂層遍歷完畢之後,下一層正好也造成了一個新的類linkedlist。咱們換到下一層之後繼續遍歷,直到最後。

 

Java:

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) {
        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;
            }
        }
    }
}

 

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) {
        if (root == null) return;
        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) {
                root = newLevel.next;
                newLevel.next = null;
                curLevel = newLevel;
            }
         }
        
    }
}

 

 

Reference:

http://www.cnblogs.com/springfor/p/3889327.html

https://leetcode.com/discuss/67291/java-solution-with-constant-space

https://leetcode.com/discuss/60795/o-1-space-o-n-time-java-solution

https://leetcode.com/discuss/24398/simple-solution-using-constant-space

https://leetcode.com/discuss/65526/ac-python-o-1-space-solution-12-lines-and-easy-to-understand

https://leetcode.com/discuss/3339/o-1-space-o-n-complexity-iterative-solution

相關文章
相關標籤/搜索