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

思路:

类似
http://blog.csdn.net/lanxu_yy/article/details/11882343。只是在寻找子节点的时候需要多一些考虑。使用递归的方法可以使代码更加简介。

代码:

非递归方法:

/**
 * 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) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(root != NULL)
        {
            build(root);
            if(root->left != NULL)
            {
                connect(root->left);
            }
            if(root->right != NULL)
            {
                connect(root->right);
            }
        }
    }
    
    void build(TreeLinkNode * root)
    {
        if(root != NULL)
        {
            TreeLinkNode * parent = root;
            TreeLinkNode * cur = NULL;
            TreeLinkNode * next = NULL;
            int isLeft = 0;
            do
            {
                while(cur == NULL && parent != NULL)
                {
                    if(parent->left != NULL)
                    {
                        isLeft = 1;
                        cur = parent->left;
                        break;
                    }
                    if(parent->right != NULL)
                    {
                        isLeft = 2;
                        cur = parent->right;
                        break;
                    }
                    parent = parent->next;
                }
                if(isLeft == 2 && parent != NULL)
                {
                    parent = parent->next;
                    isLeft = 0;
                }
                while(next == NULL && parent != NULL)
                {
                    if(parent->left != NULL && isLeft != 1)
                    {
                        isLeft = 1;
                        next = parent->left;
                        break;
                    }
                    if(parent->right != NULL)
                    {
                        isLeft = 2;
                        next = parent->right;
                        break;
                    }
                    if(isLeft == 1)
                    {
                        isLeft = 0;
                    }
                    parent = parent->next;
                }
                if(cur != NULL && next != NULL)
                {
                    cur->next = next;
                    cur = next;
                    next = NULL;
                }
            }
            while(parent != 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) {
        while((root = getChildren(root, NULL)) != NULL);
    }
    
    TreeLinkNode* getChildren(TreeLinkNode* node, TreeLinkNode* cur){
        if(node == NULL){
            return NULL;
        }

        TreeLinkNode* firstChild = NULL;
        if(node->left != NULL){
            setCurrentNode(cur, node->left);
            if(firstChild == NULL){
                firstChild = cur;
            }
        }
        
        if(node->right != NULL){
            setCurrentNode(cur, node->right);
            if(firstChild == NULL){
                firstChild = cur;
            }
        }
        
        TreeLinkNode* tmp = getChildren(node->next, cur);
        if(firstChild == NULL){
            firstChild = tmp;
        }
        return firstChild;
    }
    
    void setCurrentNode(TreeLinkNode* &cur, TreeLinkNode* next){
        if(cur == NULL){
            cur = next;
        }
        else{
            cur->next = next;
            cur = next;
        }
    }
};
    原文作者:Allanxl
    原文地址: https://blog.csdn.net/lanxu_yy/article/details/11882821
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞