Simple recursive yet efficient in cpp


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

Log in to reply
 

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