Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Logic :used BFS,stored nodes in the deque and made all same layer node's nextright node to right adjacent node of same layer.
- //TC O(N) traversed all node once
- class Solution
- {
- public:
- //Function to connect nodes at same level.
- void connect(Node *root){
- if(!root) return;
- deque<Node*> q;
- q.push_back(root);
- while(!q.empty()){
- int k=q.size();
- for(int i=0;i<k;i++){
- Node *temp=q.front();
- q.pop_front();
- if(i==k-1) temp->nextRight=NULL;
- else if(q.size()) temp->nextRight=q.front();
- if(temp->left) q.push_back(temp->left);
- if(temp->right) q.push_back(temp->right);
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement