Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- // Definition for a Node.
- class Node {
- public:
- int val;
- Node* left;
- Node* right;
- Node* next;
- Node() : val(0), left(NULL), right(NULL), next(NULL) {}
- Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
- Node(int _val, Node* _left, Node* _right, Node* _next)
- : val(_val), left(_left), right(_right), next(_next) {}
- };
- */
- class Solution {
- public:
- Node* connect(Node* root) {
- if(!root) return NULL;
- root->next = NULL;
- auto head = root;
- while(root && root->left){
- auto nextLevel = root->left;
- auto nextNode = root->left;
- while(root){
- nextNode->next = root->right;
- nextNode = nextNode->next;
- nextNode->next = (root->next) ? root->next->left : NULL;
- root = root->next;
- nextNode = nextNode->next;
- }
- root = nextLevel;
- }
- return head;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment