117. Populating Next Right Pointers in Each Node II

题目:

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
Hide Tags
 Tree Depth-first Search 

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

题解:

一样是DFS。跟上题不同在于,给定输入不是完全二叉树了,所以要加入一些条件来判断是否存在一个合理的next值。并且对左节点和右节点的有效性也要验证。最后要先递归连接右节点,再connect左节点。

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

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

原文地址:https://www.cnblogs.com/yrbbest/p/4437465.html