LeetCode 题解(61): 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
题解:不完全二叉树。自写不简便解法:

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if(!root)
            return;
        TreeLinkNode* temp = root;
        while(temp->next)
            temp = temp->next;
        temp->next = NULL;
        
        TreeLinkNode* newRoot = find(root, NULL);
        TreeLinkNode* preNode = find(root, NULL);
        while(preNode) {
            TreeLinkNode* nextNode = find(root, preNode);
            preNode->next = nextNode;
            preNode = nextNode;
        }
        connect(newRoot);
        return;
    }
    
    TreeLinkNode* find(TreeLinkNode* root, TreeLinkNode* first) {
        TreeLinkNode* result = NULL;
        if(!first) {
            while(root && !result) {
                if(root->left) {
                    result = root->left;
                } else if(root->right) {
                    result = root->right;
                } else {
                    root = root->next;
                }
            }
        } else {
            while(root && !result) {
                if(root->left == first) {
                    if(root->right) {
                        result = root->right;
                    } else {
                        root = root->next;
                        while(root && !result) {
                            if(root->left) {
                                result = root->left;
                            } else if(root->right) {
                                result = root->right;
                            } else {
                                root = root->next;
                            }                            
                        }
                    }
                } else if(root->right == first) {
                    root = root->next;
                    while(root && !result) {
                        if(root->left) {
                            result = root->left;
                        } else if(root->right) {
                            result = root->right;
                        } else {
                            root = root->next;
                        }
                    }
                } else {
                    root = root->next;
                }
            }
        }
        return result;
    }
};

网上学来简便解法:注意一定要先处理右子树再处理左子树。

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if(!root)
            return;
        
        TreeLinkNode* rootNext = root->next;
        TreeLinkNode* next = NULL;
        
        while(rootNext && !next) {
            if(rootNext->left)
                next = rootNext->left;
            else if(rootNext->right)
                next = rootNext->right;
            else
                rootNext = rootNext->next;
        } 
        
        if(root->right) {
            root->right->next = next;
            if(root->left)
                root->left->next = root->right;
        } else {
            if(root->left)
                root->left->next = next;
        }

        connect(root->right);
        connect(root->left);
    }
};

Java版:

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

Python版:

class Solution:
    # @param root, a tree node
    # @return nothing
    def connect(self, root):
        if root == None:
            return
        
        rootNext = root.next
        next = None
        
        while rootNext != None and next == None:
            if rootNext.left != None:
                next = rootNext.left
            elif rootNext.right != None:
                next = rootNext.right
            else:
                rootNext = rootNext.next
                
        if root.right != None:
            root.right.next = next
            if root.left != None:
                root.left.next = root.right
        else:
            if root.left != None:
                root.left.next = next
                
        self.connect(root.right)
        self.connect(root.left)

你可能感兴趣的:(Algorithm,LeetCode)