C++ || Efficient || Recursive || Iterative using Queue || 2 solutions ||
If you understand the approach please please upvote!!!\uD83D\uDC4D\nThanks :)\n##### Recursive Approach :-\n Base case: if the root is null than return null\n N
##### If you understand the approach please please upvote!!!\uD83D\uDC4D\n***Thanks :)***\n##### Recursive Approach :-\n* Base case: if the root is null than return null\n* Now to connect the left subtree of same level with right subtree of that level\n* The only new line that differentiate from level order traversing is that we need to connect the rightmost node of a level to the leftmost node of the next level.\n* Now just repeat the steps over and over for every level of tree .*\n##### Recursive Code:-\n```\nclass Solution {\npublic:\n    Node* connect(Node* root) {\n    if(root == NULL) return NULL;\n    //connects the left subtree of same level with right subtree of that same level \n    if(root->left != NULL) root->left->next = root->right;\n    //connect the rightmost node of a level to the leftmost node of the next level.\n    if(root->right != NULL && root->next != NULL) root->right->next = root->next->left;\n    //recursive calls for left and right subtrees.\n    connect(root->left);\n    connect(root->right);\n    return root;\n    }\n};\n```\n\n##### Iterative Approach using Queue(like level-order-traversal):-\n```\nclass Solution {\npublic:\n    Node* connect(Node* root) {\n        if(root == NULL) return NULL;\n        queue<Node*> q;\n        q.push(root);\n        while(!q.empty()){\n            int size = q.size(); // get size of queue \n            for(int i=0 ; i < size ; i++){\n                Node* item = q.front(); \n                if(size - 1 == i) // checking the last value of the level\n                     item -> next = NULL; \n                q.pop();\n                \n                if(size - 1 != i) // if this is not the last value then previous value will point to next one\n                     item -> next = q.front(); \n                \n                if(item -> left != NULL)\n                    q.push(item -> left);\n                if(item -> right != NULL)\n                    q.push(item -> right);\n            }\n        } \n        return root;\n    }\n};\n```\n
['Breadth-First Search', 'Recursion', 'Queue', 'C', 'C++']