Simple C++ Recursive Solution/ O(1) memory /25ms


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

Log in to reply
 

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