`

Populating Next Right Pointers in Each Node II

    博客分类:
  • BFS
 
阅读更多
Follow up for problem "Populating Next Right Pointers in Each Node".

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:
         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL

[分析] 这题和它的基础版本主体思路一致,BFS, 难点在于叶子节点可能出现在任何一层,在为某个节点赋予next指针时需要费工夫需找一番,按这个思路实现了Method1, 花费了近两小时才调对。翻看了自己的历史记录,看到Method2,代码精简一半,目测是网上找的答案~
yb君指出两个实现思路是一致的,Method1不断优化就能得到Method2, 漂亮的代码不是一蹴而就的,需要优化迭代~

public class Solution {
    // Method 2
    public void connect(TreeLinkNode root) {
       while(root != null){
           TreeLinkNode nextLevel = null;//first node of the next level
           TreeLinkNode prevNode = null; //previous node in the same level
           while(root != null){
                if(nextLevel == null)
                    nextLevel = root.left == null ? root.right : root.left;
                
                if(root.left != null){
                    if(prevNode != null)
                        prevNode.next = root.left;
                    prevNode = root.left;
                }
                if(root.right != null){
                    if(prevNode != null)
                        prevNode.next = root.right;
                    prevNode = root.right;
                }
                root = root.next;
           }
           root = nextLevel;
       }
    }
    //Method 1
    public void connect(TreeLinkNode root) {
        if (root == null) 
            return;
        root.next = null;
        TreeLinkNode curr = root, next = curr.next;
        TreeLinkNode startNodeOfNextLevel = curr.left != null ? curr.left : curr.right;
        while (curr != null && startNodeOfNextLevel != null) {
            // skip leaf node in the current level
            if (curr.left == null && curr.right == null) {
                if (next != null) {
                    curr = next;
                } else {
                    curr = startNodeOfNextLevel;
                    startNodeOfNextLevel = getStartNodeOfNextLevel(curr);
                    if (startNodeOfNextLevel == null) 
                        break;
                } 
                next = curr.next;
                continue;
            } 
            // search the next available node in the next level
            TreeLinkNode nextCand = null;
            while (next != null && nextCand == null) {
                if (next.left != null)
                    nextCand = next.left;
                else if (next.right != null)
                    nextCand = next.right;
                else
                    next = next.next;
            } 
            
            if(curr.left != null) {
                curr.left.next = (curr.right != null) ? curr.right : nextCand;
            }
            if (curr.right != null) {
                curr.right.next = nextCand;
            }
            
            if (nextCand == null) { // curr is the last non leaf node of the current level
                curr = startNodeOfNextLevel;
                startNodeOfNextLevel = getStartNodeOfNextLevel(curr);
                if (startNodeOfNextLevel == null)
                    break;
            } else { // curr is not the last node
                curr = next;
            }
            next = curr.next;
        }
    }
    private TreeLinkNode getStartNodeOfNextLevel(TreeLinkNode p) {
        TreeLinkNode startNodeOfNextLevel = null;
        while (p != null) {
            if (p.left != null) {
                startNodeOfNextLevel = p.left;
                break;
            } else if (p.right != null) {
                startNodeOfNextLevel = p.right;
                break;
            } else {
                p = p.next;
            }
        }
        return startNodeOfNextLevel;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics