Another approach 1st Use deque 2nd do level order traversal and when level is even (1 based index) reverse print elements otherwise simple print elements
@tarunbisht80163 жыл бұрын
yeah 2nd one is great
@khushankmadaan94073 жыл бұрын
// For the above solution, you can just perform the simple level order traversal. Considering 1 based indexing // If level is odd then then just print the elements. //If level is even just use a stack and push the elements into it and once level is traversed start popping and print the values. You will get values in reverse order because of LIFO nature of Stack. //And finally check at the end if stack still contains elements, empty the stack and print the element. vector ZigZag(Node *root) { queue q; stack st; vector ans; q.push(root); q.push(NULL); int level = 1; while (!q.empty()) { Node *x = q.front(); q.pop(); if (x != NULL) { if (level % 2 == 1) { ans.push_back(x->data); } else { st.push(x->data); } if (x->left) { q.push(x->left); } if (x->right) { q.push(x->right); } } else if (!q.empty()) { q.push(NULL); while (!st.empty()) { ans.push_back(st.top()); st.pop(); } level++; } } while (!st.empty()) { ans.push_back(st.top()); st.pop(); } return ans; }
@mandeep21923 жыл бұрын
why u push NULL in queue in 4th line
@khushankmadaan94073 жыл бұрын
@@mandeep2192 To specify that one level has been traversed completely. At first level there is only one node so, i pushed Null just after pushing the Root
@mandeep21923 жыл бұрын
@@khushankmadaan9407 i got it bro thnx nice code.
@keshavraghav38963 жыл бұрын
i have got the same intuition of check for odd or even, but i find difficulty in writting the code 😓... thnx for this one.
@manabsaha53363 жыл бұрын
Nice
@005_adarshprajapati92 жыл бұрын
just fall in love with background music
@jaychotalia75423 жыл бұрын
The Example used to do a dry run is not a BST. Because 6 is greater than 4, then also it is included in the left subtree.
@adityabhandari66883 жыл бұрын
yes, I think the link of node with data 4 was attached mistakenly to right of 2 instead of left to 3
@rutvikrana5123 жыл бұрын
We can use deque also instead of using two stacks 🙂 void zigzag(Node* root){ deque Q; Q.push_back(root); Q.push_back(NULL); bool ltr = true; while(Q.size()>1){ if(ltr){ Node* n = Q.front(); if(n){ std::cout data left)Q.push_back(n->left); if(n->right)Q.push_back(n->right); Q.pop_front(); } else{ ltr = !ltr; } } else{ Node* n = Q.back(); if(n){ std::cout data right)Q.push_front(n->right); if(n->left)Q.push_front(n->left); Q.pop_back(); } else{ ltr = !ltr; } } } cout
@EverythingaboutTechPro3 жыл бұрын
Cool ! But we can simply use Level order traversal and reverse the vector that we make (alternatively)
@tejasjoshi19073 жыл бұрын
@@EverythingaboutTechPro perfect
@tarunbisht80163 жыл бұрын
@@EverythingaboutTechPro even there is no need of reverse like for alternative row just store elements from backward
@kumarivandana15543 жыл бұрын
I think no need to check if temp is NULL or not in while loop because we already checked in the first line if root is NULL then return and the Nodes are pushed only when they are not NULL
Respected Sir/Mam Please make a video on how to practice Coding and solve ques from which platform and how to revise ques in coding because practice from geeks for geeks is difficult for me how to properly Practice particular ques and topics i am revising ques of array though i am not able to solve ques of geeks for geeks hacker earth and all the course is going bit we have to study also for backportion so how to revise and solve ques please make a dedicated video 🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏
@rutvikrana5123 жыл бұрын
All the gfg practice questions are also in their feeds, we can just simply learn from that, besides gfg practice window has option to use hints if necessary, why do you need videos of everything ? Just read gfg posts you will learn more than this whole channel's content.
@manitianajay453 жыл бұрын
que attempt karne se phle bhul ja sab stack binary tree kuchh mat soch aaram se padh aur soch tu usse kaise solve karega jaise tu solve karega waise waise kaam tere code ko bata de bas time comp aur space ke baare main sochna baaki ho jaayega sab
@051-rahulchourasia83 жыл бұрын
the animated example is not abst
@santoshbhardwaj5363 жыл бұрын
Sir java ka animated course kabtak aayga
@sarthaktyagii76833 жыл бұрын
good work team
@30jayrathi598 ай бұрын
It is not BSt zigzag traversal it is binary tree zigzag traversal
@avronilbanerjee53022 жыл бұрын
Suddenly the bass increased in the music ??
@yogesh14783 жыл бұрын
whta is the name of song in intro
@aakashthakre2443 жыл бұрын
Feeling demotivated because of not able to solve prblms on platforms
@melodytunes22043 жыл бұрын
tension mat le bhai tu akela nhi
@aritraroy63753 жыл бұрын
Bhaiya kya aap unacademy me 12th ka batch suru karenge¿??????? Please answer I am in very dipression for this 😭😭😭😭😭🙏🙏🙏
@051-rahulchourasia83 жыл бұрын
the example is not bst
@ashuchauhan3234 Жыл бұрын
useless approch
@adwaiymahajan89533 жыл бұрын
Bhaiya please reply to me .🙏🙏🙏 I really want to work with you , I want to help you . I am in 10th class now and good in almost every subject specially science and math. Please tell me if I could contribute 🙏🙏
@ajaytanwar5082 жыл бұрын
bad approach
@shubhamgoel39982 жыл бұрын
void zigZag2(node *root) { if (root==NULL) { return; } deque d1; bool ltr = false; d1.push_front(NULL); d1.push_front(root); while (!d1.empty()) { if (ltr) { node *temp = d1.back(); d1.pop_back(); if (temp!=NULL) { cout data right != NULL) { d1.push_front(temp->right); } if (temp->left != NULL) { d1.push_front(temp->left); } } else if (!d1.empty()) { d1.push_back(NULL); ltr = !ltr; } } else { node *temp = d1.front(); d1.pop_front(); if (temp!=NULL) { cout data left != NULL) { d1.push_back(temp->left); } if (temp->right != NULL) { d1.push_back(temp->right); } } else if (!d1.empty()) { d1.push_front(NULL); ltr = !ltr; } } } }