C++ concise recursion solution


  • 0
    V
    class Solution {
    public:
    
        void connect(TreeLinkNode *root) {
            if(!root) return;
            if(root->left) root->left->next=root->right?root->right:nextnode(root->next);
            if(root->right) root->right->next=nextnode(root->next);
            connect(root->right);       
            connect(root->left);
        }
        TreeLinkNode* nextnode(TreeLinkNode* node) {
            if(!node) return NULL;
            return node->left?node->left:node->right?node->right:nextnode(node->next);
        }
    
    };
    

Log in to reply
 

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