[LeetCode] 116. Populating Next Right Pointers in Each Node

Keywords: IE

Populating Next Right Pointers in Each Node

Given a binary tree

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
Note:
You may only use constant extra space.
Recursive approach is fine, implicit stack space does not count as extra space for this problem.
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
Example:

analysis

Suppose that the binary tree is a full binary tree, that is, each node has two sub-nodes, and all leaf nodes are on the same level, linking the next of each node to the adjacent right node.

Solution 1: Queue

If there are child nodes, connect the next of the left node to the right node.
If the next of the node is not empty, link the next of the right node to node - > next - > left, and link to NULL if it is empty.

class Solution {
public:
    void connect(TreeLinkNode *root) {
        queue<TreeLinkNode* > q;
        if(!root) return;
        q.push(root);
        while(!q.empty()){
            TreeLinkNode* node = q.front();
            q.pop();
            if(node->left && node->right){
                node->left->next = node->right;
                if(node->next)
                    node->right->next = node->next->left;
                else 
                    node->right->next = NULL;
                q.push(node->left);
                q.push(node->right);
            }
        }
    }
};

Layered by queue size, the next of the current node points to the queue head pointer when the hierarchical traversal is not the last node of each layer.

class Solution {
public:
    void connect(TreeLinkNode *root) {
        if (!root) return;
        queue<TreeLinkNode*> q;
        q.push(root);
        while (!q.empty()) {
            int size = q.size();
            for (int i = 0; i < size; ++i) {
                TreeLinkNode *t = q.front(); 
                q.pop();
                if (i < size - 1) {
                    t->next = q.front();
                }
                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
            }
        }
    }
};

Solution 2: Recursion

Direct recursive judgment of left and right subtrees and links

class Solution {
public:
    void connect(TreeLinkNode *root) {
        if (!root) return;
        if (root->left && root->right) {
            root->left->next = root->right;
            root->right->next = root->next? root->next->left : NULL;
        }
        connect(root->left);
        connect(root->right);
    }
};

Solution 3: Double Pointer, Spatial Complexity O(1)

Start and cur are used with two pointers, where start marks the starting node of each layer and cur is used to traverse the nodes of that layer.

class Solution {
public:
    void connect(TreeLinkNode *root) {
        if (!root) return;
        TreeLinkNode *start = root, *cur = NULL;
        while (start->left) {
            cur = start;
            while (cur) {
                cur->left->next = cur->right;
                if (cur->next) cur->right->next = cur->next->left;
                cur = cur->next;
            }
            start = start->left;
        }
    }
};

Reference resources

http://www.cnblogs.com/grandyang/p/4288151.html

Posted by TimC on Sun, 03 Feb 2019 15:06:16 -0800