solution.cpp 979 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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 and (root->left or root->right))
        {
            if (root->left and root->right)
                root->left->next = root->right;
            for (TreeLinkNode *cur = root, *find = root->next; find; find = find->next)
            {
                if (!find->left and !find->right)
                    continue;
                if (find->left and find->right)
                    find->left->next = find->right;
                (cur->right ? cur->right->next : cur->left->next) = find->left ? find->left : find->right;
                cur = find;
            }
            root = root->left ? root->left : root->right;
            while (root->next && !root->left and !root->right)
                root = root->next;
        }
    }
};