5 lines C++ simple solution.


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.