My simple C++ Solution, 19ms, beat 94.20%


  • 0
    W
    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            helper(root);
            TreeLinkNode *p=root;
            while(p){
                p->next = nullptr;
                p = p->right;
            }
        }
        void helper(TreeLinkNode *root){
            if(!root||(!root->left&&!root->right)) return;
            TreeLinkNode *p = root->left, *q = root->right;
            while(p&&q){
                p->next = q;
                p = p->right;
                q = q->left;
            }
            helper(root->left);
            helper(root->right);
        }
    };
    

Log in to reply
 

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