Leetcode: 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

차원에 따라 계속 사용할 수 있지만, constant extra 공간은 아닙니다.
/**
 * 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 == NULL) return;  
          
        TreeLinkNode *cur = NULL;  
        queue<TreeLinkNode*> trees;  
        trees.push(root);  
        trees.push(NULL);  
        while (!trees.empty()) {  
            cur = trees.front();  
            trees.pop();  
            if (cur != NULL) {  
                cur->next = trees.front();  
                if (cur->left != NULL) {  
                    trees.push(cur->left);
                }
                if (cur->right != NULL) {
                    trees.push(cur->right);  
                }  
            }  
            else if (!trees.empty()) {  
                trees.push(NULL);  
            }  
        }  
    }
};

I에 대응하는 두 번째 방법:
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if (root == NULL || root->left == NULL && root->right == NULL) {  
            return;  
        }  
        
        connect(root->left);  
        connect(root->right);  
        
        int level = 0;
        while (true) {
            TreeLinkNode *most_right = mostRight(root->left, level);
            TreeLinkNode *most_left = mostLeft(root->right, level);
            if (most_right != NULL && most_left != NULL) {
                most_right->next = most_left;
                ++level;
            }
            else {
                break;
            }
        } 
    }
    
    TreeLinkNode *mostRight(TreeLinkNode* root, int level) {
        if (root == NULL) {
            return NULL;
        }
        else if (level == 0) {
            return root;
        }
        
        TreeLinkNode *right = mostRight(root->right, level - 1);
        if (right == NULL) {
            right = mostRight(root->left, level - 1);
        }
        
        return right;
    }
    
    TreeLinkNode *mostLeft(TreeLinkNode* root, int level) {
        if (root == NULL) {
            return NULL;
        }
        else if (level == 0) {
            return root;
        }
        
        TreeLinkNode *left = mostLeft(root->left, level - 1);
        if (left == NULL) {
            left = mostLeft(root->right, level - 1);
        }
        
        return left;
    }
};
I에 대응하는 세 번째 방법은 기본적으로http://blog.csdn.net/fightforyourdream/article/details/16854731참고하여 오다.공부했어.
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if (root == NULL || root->left == NULL && root->right == NULL) {  
            return;  
        }  
        
        TreeLinkNode *rnext = root->next;
        TreeLinkNode *next = NULL;
        while (rnext != NULL && next == NULL) {
            if (rnext->left != NULL) {
                next = rnext->left;
            }
            else if (rnext->right != NULL) {
                next = rnext->right;
            }
            rnext = rnext->next;
        }
        
        if (root->left != NULL) {
            if (root->right != NULL) {
                root->left->next = root->right;
            }
            else {
                root->left->next = next;
            }
        }
        if (root->right != NULL) {
            root->right->next = next;
        }
        
        connect(root->right);
        connect(root->left);  
    }
};

================= 두 번째 ==================
/**
 * 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 == NULL || root->left == NULL && root->right == NULL) {
            return;
        }
        
        TreeLinkNode* childNext = NULL;
        TreeLinkNode* next = root->next;
        while (childNext == NULL && next != NULL) {
            if (next->left != NULL) {
                childNext = next->left;
            }
            else if (next->right != NULL) {
                childNext = next->right;
            }
            next = next->next;
        }
        
        if (root->left != NULL) {
            root->left->next = root->right ? root->right : childNext;
        }
        if (root->right != NULL) {
            root->right->next = childNext;
        }
        
        connect(root->right);
        connect(root->left);
    }
};

좋은 웹페이지 즐겨찾기