Easy to understand c++ solution


  • 1
    A
    class Solution {
        public:
        void fn(TreeLinkNode *a,TreeLinkNode *b,TreeLinkNode *c)
        {
            if(a==NULL||b==NULL)
            return;
            a->next=b;
            b->next=c;
            fn(a->left,a->right,b->left);
            if(c!=NULL)
            fn(b->left,b->right,c->left);
            else
            fn(b->left,b->right,NULL);
            
            return ;
        }
    public:
        void connect(TreeLinkNode *root) {
            if(root==NULL)
            return ;
            root->next=NULL;
            fn(root->left,root->right,NULL);
            return ;
        }
    };
    

Log in to reply
 

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