This channel is literally underrated, I watched Morris Traversal from Striver as well as Love Babbar, but always found it difficult. This tutorial is literally the best explanation on Morris Traversal.
@lucifersamrat62803 ай бұрын
exactly same situation
@mohamedameen91173 ай бұрын
same here
@itshirdeshk8 ай бұрын
Postorder Traversal vector postOrder(Node* root) { // code here // We will traverse the tree in the order - N R L. vector ans; // We will traverse the tree until our root becomes nuLL that shows we have traverse the whole tree. while(root) { // If right of root doesn't exist then we will just go the left side and push the value of the node into the array. if(!root -> right){ ans.push_back(root -> data); root = root -> left; } // If right of root exist: else { // So if the right exist then we have to: // first, create a link of the leftmost node to the root node. // So that we can come back to root node. Node *curr = root -> right; // We are going to the leftmost node. while(curr -> left && curr -> left != root) curr = curr -> left; // So now there are two conditions: // First, that we have not traverse that right [part. // Second, we have traverse the right part. // Now if we have not traverse the part then the left value of the curr node is NULL. // So we will add the value of the node to the ans array. // And make a link and go the right. if(curr -> left == NULL){ ans.push_back(root -> data); curr -> left = root; root = root -> right; } // If we done the traversal then we will simply remove the link and go to left part. else { curr -> left = NULL; root = root -> left; } } } // After that we will reverse the ans array. reverse(ans.begin(), ans.end()); return ans; }
@pranavmittal96198 ай бұрын
thanks bro
@sakshamsharma31565 ай бұрын
Literally the best tutorial of Morris Traversal on KZbin . Thanks sir!
@ankushladani4968 ай бұрын
Pehle mene pseudocode likha and badme code implement Kiya. Post Order Code Done ✅
@anupammishra28918 ай бұрын
pura chamak gya bhaiya morris traversal pura ache se best explaination...........🤗🤗🤗🤗🤗
@itshirdeshk8 ай бұрын
Day 161 ✅🔥 Learning++ bhaiya ✨
@jagmaggarg6901Күн бұрын
bhaiya one of the finest explanation of morris traversal.you cover in depth and now it will stay in mind forever .and also done postorder traversal .ek dam chamak gaya. code: vector postorderTraversal(TreeNode* root) { vectorans; if(!root) return ans; TreeNode* curr=root; while(curr){ //right part not exist if(curr->right == NULL){ ans.push_back(curr->val); curr=curr->left; } //right part exist else{ TreeNode* pred=curr->right; while(pred->left && pred->left!=curr){ pred=pred->left; } //if link not establish means right tree not traversed if(pred->left==NULL){ ans.push_back(curr->val); pred->left=curr; curr=curr->right; } //link established means tree already traversed else{ pred->left=NULL; curr=curr->left; } } } reverse(ans.begin(),ans.end()); return ans; }
@sharadjishukla32974 ай бұрын
// Approach 3 : Morris Traversal : TC:O(n) SC:O(1) vector postorderTraversal(TreeNode *root) { vector ans; TreeNode *curr = root; // solving for (NRL) while (curr) { // right doesnt exist, noteNode, goLeft if (!curr->right) { ans.push_back(curr->val); curr = curr->left; } // right exist else { TreeNode *post = curr->right; // check link while (post->left && post->left != curr) post = post->left; // noLink, notTraversed, createLink, noteNode, goRyt if (!post->left) { post->left = curr; ans.push_back(curr->val); curr = curr->right; } // Link, Traversed, destroyLink, goLeft else { post->left = NULL; curr = curr->left; } } } // reverse ans to get postorder (LRN) from (NRL) reverse(ans.begin(), ans.end()); return ans; }
@devendrasharma8750Ай бұрын
bhaiya easily understood and chamak gaya sab, thank you bhayia
@ManojSinghRawat-x2w8 ай бұрын
post order using morris traversal // using morris traversal class Solution{ public: vector postOrder(Node* root) { // code here vectorans; while(root){ // left part doesn't exist if(!root->right){ ans.push_back(root->data); root=root->left; } // left part exist else{ Node*curr=root->right; while(curr->left && curr->left!=root) curr=curr->left; // left subtree not traverse if(curr->left==NULL){ ans.push_back(root->data); curr->left=root; root=root->right; } // already exist else{ curr->left=NULL; root=root->left; } } } reverse(ans.begin(),ans.end()); return ans; } }; 👍👍acha lage toh 😊
@codedByAyush2 ай бұрын
Best explanation on Morris Traversal ❤
@ramdeoyadav83693 ай бұрын
maza a gya video dhekh ke learning first time morris traversal
@developer999Ай бұрын
Morris PostOrder Traversal code vectorans; // store answer while(root){ // check if right substree exits or not if(!root->right){ ans.push_back(root->data); root = root->left; // go to left subtree for further processing } else{ // when left substree exist Node* curr = root->right; // create instance while(curr->left && curr->left != root){ curr = curr->left; } // after reaching last node we connect last node with root node curr->left = root->left; ans.push_back(root->data); root = root->right; // move to right for further processing } } reverse(begin(ans) , end(ans)); return ans;
//in postorder traversal follow(LEFT-RIGHT-NODE) in i reverse this technique which is (NODE-RIGHT-LEFT) and at end i reverse the all value of array then return: class Solution { public: vector postorderTraversal(TreeNode* root) { vector ans; // Stores result while (root) { //when right child of root node is not exist if (!root->right) { ans.push_back(root->val); root = root->left; } // when right child of root node is exist else { TreeNode* curr = root->right; // Find leftmost node in right subtree while (curr->left && curr->left != root) { curr = curr->left; } // Create link ,when right subtree of root node of left child's left part is null then its in left ptr to link root node for for accesss that element reverse time if (!curr->left) { ans.push_back(root->val); curr->left = root; // Make link root = root->right; } // Remove link,when the work is done that node then link is remove else { curr->left = NULL; // Remove link root = root->left; } } } // Reverse the answer array for to get correct order reverse(ans.begin(), ans.end()); return ans; } };
@Sahilsharma-sk5vr3 ай бұрын
watching your channelf or the first time and i am really impressed.
@chirag_mehra54 ай бұрын
We solve postorder by reversing the NRL order which will be a mirror NLR i.e preorder , hence the solution will be vectorans; while(root){ if(!root->right){ ans.push_back(root->data); root = root->left; } else{ Node *curr = root->right; while(curr->left && curr->left != root) curr = curr->left; if(curr->left == NULL){ ans.push_back(root->data); curr->left = root; root = root->right; } else{ curr->left = NULL; root = root->left; } } } reverse(ans.begin(),ans.end()); return ans;
@ankushladani4968 ай бұрын
Bhaiya Aaj Jo aapne pseudo code likhwaya Wo acha laga ase hi continue karna Taki hame aadat ho Jaye uske bad aapko nahi likhne denge 😅😂
@ankushladani4968 ай бұрын
Done Bhaiya... Day 161/180 ✅💯
@AnoopRajpoot-i3jАй бұрын
class Solution{ public: vector postOrder(Node* node) { vectorans; while(node) { //right part does not exist if(!node->right) { ans.push_back(node->data); node=node->left; } //right part exist else { Node* curr=node->right; while(curr->left && curr->left!=node) curr=curr->left; //right part not traversed if(curr->left==NULL) { curr->left=node; ans.push_back(node->data); node=node->right; } //right part traversed else { curr->left=NULL; node=node->left; } } } //conversion from NRL to LRN int start=0,end=ans.size()-1; while(start
@lajpalarjun8 ай бұрын
Very well explained. Enjoyed the video a lot. Thank you so much.
@rrr98488 ай бұрын
Learning++ bhaiya 🎉
@CoderArmy98 ай бұрын
Keep Going
@AKASHKUMAR-li7li5 ай бұрын
Best explanation of morris traversal you ever find♥ learning++
@mohamedameen91173 ай бұрын
Thank you so much bhaiya, your explanations are very easy to understand ❤
@heetpatel30375 ай бұрын
Jai Shri Ram ❤️ Thank you Bhaiya 👍🏻 Ek dum op content
@vaishnavigoswami57075 ай бұрын
class Solution{ public: vector postOrder(Node* root) { vectorans;vectora; while(root) { // first see if right doesnt exist then simply go to left if(root->right==NULL) {ans.push_back(root->data); root= root->left; } else {//if right exist check if link is present or it the left of node isNULL Node* p = root->right; while(p->left&&p->left!=root) { p = p->left; } //if p->left = null means visiting it for first time so push the answer then create ,link and move the pointer to right if(p->left==NULL) { ans.push_back(root->data); p->left= root; root= root->right; } //if link exist then break the link and move to the left part sice we have traversed the right part else { p->left=NULL; root= root->left; } } } // reverse the answer for(int i = ans.size()-1;i>=0;i--) { a.push_back(ans[i]); } return a; } };
@tradun.s97605 ай бұрын
best lectures on allover youtube
@harshvardhanrathore77512 ай бұрын
class Solution { public: vector postorderTraversal(TreeNode* root) { vector ans; while (root) { if (!root->right) { // right doesn't exist ans.push_back(root->val); root = root->left; } else { // right part exist TreeNode* curr = root->right; while (curr->left && curr->left != root) { curr = curr->left; } if (curr->left == NULL) { // right part doesn't traverse ans.push_back(root->val); curr->left = root; root = root->right; } else { // already traverse curr->left = NULL; root = root->left; } } } reverse(ans.begin(),ans.end()); return ans; } };
@nitinverma19028 ай бұрын
class Solution{ public: vector postOrder(Node* root) { // code here vectorans; //post order - LRN // reverse of post order -NRL //NODE //RIGHT //LEFT // when root exits while(root) { // when right doen't exits if(!root->right) { ans.push_back(root->data); root = root->left; } //when right exits else { Node * curr = root->right; //when right is not traverse while(curr->left && curr->left!=root) { curr = curr->left; } if(curr->left == NULL) { //store the root-> data in ans ans.push_back(root->data); //create link between last right node and root node curr->left = root; //move to right root =root->right; } // when right is already traverse else { curr->left = NULL; root = root->left; } } } //reverse it, in order to make it LRN(postorder) reverse(ans.begin(), ans.end()); return ans; } };
@rohitbisht11078 ай бұрын
Nice bhaiya ❤ Keep growing .. Full support from uttarakhand ❤❤
@abhijeetbasfore68168 ай бұрын
Post Order Moris traversal: class Solution{ public: vector postOrder(Node* root) { vector ans; // left , right, node ---> node, right, left while(root != NULL){ if(root->right == NULL){ ans.push_back(root->data); root = root->left; } else{ Node* temp = root->right; while(temp->left != nullptr && temp->left != root){ temp = temp->left; } // if leftmost of rightpart not visited if(temp->left == nullptr){ ans.push_back(root->data); temp->left = root; root = root->right; } // if the leftmost of right part already visited if(temp->left == root){ temp->left = NULL; root = root->left; } } } // To get actual result reverse(ans.begin(), ans.end()); return ans; } };
@ankitshrestha6481Ай бұрын
50:18 [Morris Traversal Post order]: void morris_traversal_postorder(Node* root) { if (!root) { return; } // Here stack is just used to store answer, not for traversing purpose // If we used vector then we need to reverse it but in stack we already get last value so print it directly stack ans; while (root) { // Is Right Exist if (root->right) { Node* temp = root->right; // Going extreme left of temp node while (temp->left && temp->left != root) { temp = temp->left; } // If temp node left point to root node means We already traversed it if (temp->left == root) { // remove linked from root to null and move to left temp->left = nullptr; root = root->left; } // We traversed first time so push value in ans stack else { ans.push(root->data); // make linked to root and move right temp->left = root; root = root->right; } } // Right Not Exist means push in ans stack and move to left else { ans.push(root->data); root = root->left; } } // printing traversed value while (!ans.empty()) { int top = ans.top(); ans.pop(); cout
@ramsiyaram100824 ай бұрын
thanks bhaiya for providing such incredible content
All other youtubers simply ignoring morris postorder without any reason except for u 🙏🙏
@skasfarali53143 ай бұрын
vector postOrder(Node* root){ vector ans; //At first we will traverse Node,Right & Left in this order.At the end we reverse the vector to get ans. while(root){ //right does not exist if(!root->right){ ans.push_back(root->data); root = root->left; } else{//right exist Node* curr = root->right; while(curr->left && curr->left != root) curr = curr->left; //if not traverse if(!curr->left){ ans.push_back(root->data); curr->left = root; root = root->right; } else{//traversed curr->left = nullptr; root = root->left; } } } reverse(ans.begin(),ans.end());//for getting actual ans return ans; }
@bharatmalik27568 ай бұрын
Rohit Bhaiya teaching dsa >> any paid course 😎
@relaxingtime24118 ай бұрын
Haan Ji Mere Aaka Good Morning ❤
@vg_858 ай бұрын
Best explanation on internet # learning++
@ritwiksaha20096 ай бұрын
/* STEPS: 1. Right part does not exist: i) note down the data ii) move to left part 2. Right part exist: i)Right subtree is not traversed yet (a) note down the data (b) create link (c) move to the right part ii)Right subtree already traversed (a) remove the link (b) move to the left part */ //Morris Traversal(Space Complexity O(1) and T.C=O(n) ) //left, right, node //1st we solve reverse way: node, right, left vector postOrder(Node* root) { vectorans; while(root){ //right part does not exist if(root->right == NULL){ ans.push_back(root->data); root = root->left; } //right part exist else{ Node *curr = root->right; while(curr->left != NULL && curr->left != root) curr = curr->left; //right subtree is not traversed if(curr->left == NULL){ ans.push_back(root->data); curr->left = root; //create link root = root->right; } //right subtree is already traversed else{ curr->left = NULL; //break the link root = root->left; } } } //Now reverse the answer reverse(ans.begin(), ans.end()); return ans; }
@RanjeetKumarceb8 ай бұрын
// Morris Travesal class Solution{ public: vector postOrder(Node* root) { vectorans; // creating vector for storing the ans while(root){ // we will run while lopp while root will not be null if(root->right==NULL){ // right does't exits ans.push_back(root->data); root = root->left ; }else{ // right exits Node* curr = root->right; // going from the right to leftmost node while(curr->left && curr->left !=root){ curr = curr->left; } if(curr->left==NULL){ // not travese curr->left = root; ans.push_back(root->data); root = root->right; }else{ // travesed curr->left = NULL; root = root->left; } } } reverse(ans.begin(),ans.end()); // NRL reverse == LRN(postorder) return ans; } };
@ramsiyaram100824 ай бұрын
Post Order trivasal By Morris Method class Solution{ public: vector postOrder(Node* root) { // code here // insert NRL then reverse which is LNR is post order vectorans; while(root){ // Right part doesn't exits if(!root->right){ ans.push_back(root->data); root=root->left; } // if right part exist karta ho else{ Node *curr=root->right; while(curr->left&&curr->left!=root){ curr=curr->left; } // right subtree is not trivased if(curr->left==NULL){ ans.push_back(root->data); curr->left=root; root=root->right; } // right subtree is trivased already else{ curr->left=NULL; root=root->left; } } } reverse(ans.begin(),ans.end()); return ans; } };
@asad_iliyas7 ай бұрын
50:20 : vector postOrder(Node* root) { // code here vectorans; // First we find NRL and then reverse it to get post order while(root) { if(!root->right)//right subtree don't exist { ans.push_back(root->data); root=root->left;//Now move to left subtree } else//right subtree exists { Node *curr=root->right; while(curr->left && curr->left != root) curr=curr->left; if(!curr->left)//right subtree not traversed { ans.push_back(root->data); curr->left=root; root=root->right; } else // Already traversed { curr->left = NULL; root=root->left; } } } reverse(ans.begin(),ans.end());//reverse to get LRN return ans; }
@kartikbohra19047 ай бұрын
Morris PostOrder Just some small changes vectorans; //left right rooot //node right left while(root) { if(!root->right) { ans.push_back(root->data); root=root->left; } else { //right part not traverse Node *curr=root->right; while(curr->left && curr->left!=root) { curr=curr->left; } //add link if(curr->left==NULL) { ans.push_back(root->data); curr->left=root; root=root->right; } //already traverse so remove link else { curr->left=NULL; root=root->left; } } } reverse(ans.begin(),ans.end()); return ans;
@zebra-er6xc7 ай бұрын
code for post order: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector postorderTraversal(TreeNode* root) { // LRN // reverse = NRL vector ans; while(root){ if(root -> right == NULL){ ans.push_back(root -> val); root = root -> left; } else { // right of root exists // so now we go to leftmost node // to check if the link exists or we have to make one TreeNode* curr = root -> right; while(curr -> left != NULL && curr -> left != root){ curr = curr -> left; } if(curr -> left == NULL){ // we have to make the link // first time on that node ans.push_back(root->val); curr -> left = root; root = root -> right; } else if(curr -> left == root){ curr -> left = NULL; // ans.push_back(root -> val); root = root -> left; // since this portion has been already visited } } } reverse(ans.begin(), ans.end()); return ans; } };
@rrr98488 ай бұрын
Using Morris Algorithem T.C->O(n) and S.C->O(1) class Solution{ public: vector postOrder(Node* root) { vectorans; // Post Order -> LRN // Reverse of Post Order -> NRL // Node // Right // Left while(root){ // Rigth part does not exist if(!root->right){ ans.push_back(root->data); root=root->left; } // Right part exist else{ Node *curr=root->right; while(curr->left && curr->left!=root){ curr=curr->left; } //Right subtree not traverse if(curr->left==NULL){ ans.push_back(root->data); curr->left=root; root=root->right; } // Already treverse else{ curr->left=NULL; root=root->left; } } } // NRL reverse to LRN reverse(ans.begin(),ans.end()); return ans; } };
@AnkitTiwari-o1s8 ай бұрын
{While(root) // to store root {ans.push _back(root->data); // right kr end tak jana aur current ke left mein jana Node*curr=root->right ; {While (root>right =null;&& root>left =null; Curr=curr->left; Curr-left=root ; root=root->left; } else{ Node *cure =root->left}
@tesla21464 ай бұрын
wonderful explanation
@thinker-o5p4 ай бұрын
ekdam chamak gaya bhaiya
@bidyasagarmohapatra40146 ай бұрын
50:20 Morris Travesal for Post order class Solution { public: /* take this example 1 / \ 2 3 / \ 4 5 (5's left connected to 3) (4's left connected to 1) */ vector postorderTraversal(TreeNode* root) { // L R N -> original // we use morris traversal to find reverse of that 1st then reverse it // N R L // code here vector revPostOrder; TreeNode* curr = root; while(curr != NULL) { // if curr right is null then visit it, then go to left (5 node) if(curr->right == NULL) { revPostOrder.push_back(curr->val); curr = curr->left; } else { // right node is not null (like 1) // 1st find predecessor, then connect the link //a. find predecessor TreeNode* pred = curr->right; while(pred->left != curr && pred->left) { pred = pred->left; } // b. check if the pred is already linked or not if(pred->left == NULL) { pred->left = curr; revPostOrder.push_back(curr->val); curr = curr->right; } else { // disconnect the link then go to left pred->left = NULL; curr = curr->left; } } } reverse(revPostOrder.begin(), revPostOrder.end()); return revPostOrder; } };
@shubhamkumarjha91928 ай бұрын
Good morning bhaiya ji. 💖
@shivanshgoel25044 ай бұрын
*CODE FOR LAST QUESTION BINARY TO DOUBLY LINKED LIST* class Solution { public: //Function to convert binary tree to doubly linked list and return it. Node * bToDLL(Node *root) { vector answer; while(root) { if(root->left==nullptr) { answer.push_back(root->data); root=root->right; } else { Node * current = root->left; while(current->right && current->right!=root) { current=current->right; } if(current->right==nullptr) { current->right=root; root=root->left; } else { current->right =nullptr; answer.push_back(root->data); root=root->right; } } } Node * dummy = new Node(-1); Node * head=dummy; for(int i=0;iright = temp; temp->left=head; head=head->right; } dummy->right->left=nullptr; return dummy->right; } };
@Prabhu_ShreeRam16 ай бұрын
{ Learning ++: Concepts = Concepts+1; }
@mr._devil87024 ай бұрын
class Solution { public: vector postorderTraversal(TreeNode* root) { vectorans; while(root) {//postorderTraversal(left->right->node) //Node->left->right=>reverse it //right leaf node dont have if(!root->right) { //value le lo or left mai jao ans.push_back(root->val); root=root->left; } else { //root ka right exist hai to root ka //sable leftmost children ko bolo root ko pont kare TreeNode *curr=root->right; while(curr->left&&curr->left!=root) { curr=curr->left; } if(curr->left==NULL) { //ham root ka left children kai leftmost last node pe hai //jo abhi null hai(1 st time ham aa rahe hai) //to root ka data push karo or loop bana do ans.push_back(root->val); curr->left=root; root=root->right; }//ham Traverse kar chuke hai,to wo loop hata do // or left mai jaoo else { curr->left=NULL; root= root->left; } } } reverse(ans.begin(),ans.end()); return ans;
@RishabhChatterjee-fg2gz4 ай бұрын
Postorder Traversal using Morris Traversal vector result; while(root) { // if right subtree does not exist if(!root->right) { // if root->right is null then store root data into result result.push_back(root->data); // after that move root to the left root = root->left; } // if right subtree exist else { Node* curr = root->right; // run loop until curr->left is null or curr->left is root while(curr->left and curr->left != root) curr = curr->left; // if curr->left is null that means link is not created, create link // with left most node, store root->data into result, and move root // to its right if(!curr->left) { curr->left = root; result.push_back(root->data); root = root->right; } // if link is created, that means right subtree is already traversed, // thenbreak the link and move root to its left subtree else { curr->left = nullptr; root = root->left; } } } // After that reverse the traversal because postorder traversal say // At first Left subtree, then Right Subtree and then Root, but here I done it // reverse, at first Root, then Right Subtree then Left Subtree. So, result // come in reverse, that's why reverse it reverse(result.begin(), result.end()); return result;
❤🔥 PostOrder: vector postOrder(Node* root) { // code here //for post order (lrn) //but in this case we do NRL //Morris Traversal vectorans; while(root) { //right part does not Exist if(!root->right) { ans.push_back(root->data); root=root->left; } else//right part exist { Node *cur=root->right; while(cur->left&&cur->left!=root) { cur=cur->left; } //right sub tree not traversed if(cur->left==NULL) { ans.push_back(root->data); cur->left=root; root=root->right; } else //alreay traversed { cur->right=NULL; root=root->left; } } } //reverse reverse(ans.begin(),ans.end()); return ans; } };
Bhaiya exam ke karan discontinue hogaya hai will be back soon ❤❤.. i have exam today but still wanted to comment i usually won't most of the times but i do care ur channel a lot❤❤
@CoderArmy98 ай бұрын
Good luck for exam bhai
@dashstar338 ай бұрын
@@CoderArmy9Acha Gaya bhai❤❤ abi do bache Hain par firbhi may aajse lecture continue karoonga🤞
@Okayboy6788 ай бұрын
Thank you for this playlist but just one question is it gonna be a complete one i mean complete DSA using C++?
@manish_kumar_iitg2 ай бұрын
38:20 Morris Traversal Pseudo cod for in-Order for revision.
@ShreeRamSingh-vl9tl5 ай бұрын
//code here //WE ARE DOING MORRIS TRAVERSAL /* Acctual Postorder Left Right Node But we do Node Right Left */ vectorans; while(root) { //Right Part doesn't exit if(!root->right) //if (root->right == NULL) { ans.push_back(root->data); root=root->left; } //Right Part exit else{ Node * curr = root->right; while(curr->left && curr->left!=root){ curr=curr->left; } //right subtree not traversal if(curr->left==NULL){ ans.push_back(root->data); curr->left=root; root=root->right; }else{ //Alreay Traversal curr->left=NULL; root=root->left; } } } //Now i will make my answer LRN reverse(ans.begin(),ans.end()); return ans;
@amybross4 ай бұрын
bhot badhiya bhaiya
@anupammishra65148 ай бұрын
post order traversal code............. class Solution{ public: vector postOrder(Node* root) { //use NRL concept //node //left //right vectorv; while(root) { //if root's right part is not exist if(!root->right) { v.push_back(root->data); root=root->left; } //if root's right part is exist else { Node * curr=root->right; while(curr->left && curr->left!=root) { curr=curr->left; } //right subtree traverse or not //if not traverse if(curr->left==NULL) { v.push_back(root->data); curr->left=root; root=root->right; } //if traverse else { curr->right=NULL; root=root->left; } } } reverse(v.begin(),v.end()); return v; } };
@OK-ku8ez7 ай бұрын
PostOrder Traversal vector postOrder(Node* root) { // code here //Morris Traversal vectorans; while(root) { //Right part doesn't exist, Store and Go left if(!root->right) { ans.push_back(root->data); root=root->left; } //Right part exists else { Node *curr=root->right; while(curr->left!=NULL && curr->left!=root) { curr=curr->left; } //Right not traversed, Store and Go right if(curr->left==NULL) { ans.push_back(root->data); curr->left=root; root=root->right; } //Right traversed, Go left else { curr->left=NULL; root=root->left; } } } reverse(ans.begin(), ans.end()); return ans; }
Morris Traversal Postorder: vector postOrder(Node* root) { // code here //postorder is left-right-node //we found its reverse that is node-right-left,and reverse //it to get the original answer vectorans; while(root) {//Morris traversal if(root->right==NULL) {//Right part is not exist ans.push_back(root->data); root=root->left; } else {//Right part exist Node *curr=root->right; while(curr->left && curr->left!=root) curr=curr->left; if(curr->left==NULL) {//Right part is not visited curr->left=root;//Creating the link ans.push_back(root->data);// Node-Right-Left root=root->right; } else {//Right part is already visited curr->left=NULL;//removing the link root=root->left; } } } //Now reverse the final array reverse(ans.begin(),ans.end()); return ans; }
@hemantpatil45758 ай бұрын
Post-order. class Solution{ public: vector postOrder(Node* node) { // code here vectorans; //node //right //left while(node) { if(!node->right) // If right does not exist { ans.push_back(node->data); node=node->left; } else //If right exist { Node *curr = node->right ;//pointer pointing to right node of the root while(curr->left && curr->left!=node) { curr=curr->left; } //right subtree not traversed if(curr->left == NULL) { ans.push_back(node->data); curr->left = node; node=node->right; } else //right subtree is traversed { curr->left = NULL; node=node->left; } } } reverse(ans.begin(),ans.end()); return ans; } };
@amannegi181719 күн бұрын
Chamak gyaa bhejii❤❤❤
@ahsankhan-rt2kh8 ай бұрын
Good morning bhaiya.
@YashSaini0078 ай бұрын
Bhaiya Radhe Radhe 🙏
@CoderArmy98 ай бұрын
Radhe Radhe bhai
@DelightfulTreeSwing-qo6tm8 ай бұрын
Haaan bhaiya chamak gya
@Code_with_Tiwarijii8 ай бұрын
Chamak gya bhaiya
@nidhiraj96867 ай бұрын
vector postorderTraversal(TreeNode* root) { //morris traversal //time O(n) space O(1) //intution is reverse of NRL vector ans; while(root){ if(!root->right) { ans.push_back(root->val); root=root->left; } else{ TreeNode* temp = root->right; while(temp->left && temp->left != root) temp=temp->left; if(!temp->left){ //right side is not traversed temp->left = root; ans.push_back(root->val); root=root->right; } else{ //right side is traversed temp->left = NULL; root=root->left; } } } reverse(ans.begin(), ans.end()); return ans; }
@Eng_wallah8 ай бұрын
Bhaiya radhe radhe 🙏🏻🙏🏻
@pranavmittal96198 ай бұрын
hukum aapka hukm sar aakho per
@swarnadeepsaha44558 ай бұрын
Bhaiya best of best❤
@Ajeetkumar-uo4hfАй бұрын
Like kar diya bhai
@ankushladani4968 ай бұрын
Thanks Bhaiya...❤🎉
@YASHYADAV-f9iАй бұрын
bhaiya learning++
@ManishSingh-m1m3 ай бұрын
chamak gya bhaiya
@kuldeepparmar98618 ай бұрын
solution of postorder traversal using morris traversal vectorans; while(root){ // if right node does not exist if(!root->right){ ans.push_back(root->data); root = root->left; } // if right node exist else{ Node*temp= root->right; while(temp->left && (temp->left!=root)){ temp = temp->left; } // if left subtree does not traverse if(temp->left==NULL){ ans.push_back(root->data); temp->left = root; root = root->right; } //if left subtree exist else{ temp->left = NULL; root =root->left; } } } reverse(ans.begin(),ans.end()); return ans;
@allinonemoviesyt8 ай бұрын
good morning bhaiya ji
@mahmudaliza4079Ай бұрын
postorder traversal class Solution { public List postorderTraversal(TreeNode root) { LinkedList res = new LinkedList(); TreeNode curr = root; while (curr != null) { if (curr.right == null) { res.add(curr.val); curr = curr.left; } else { TreeNode pre = curr.right; while (pre.left != null && pre.left != curr){ pre = pre.left; } if (pre.left == null) { res.add(curr.val); pre.left = curr; curr = curr.right; } else { pre.left = null; curr = curr.left; } } } Collections.reverse(res); return res; } }
Day 161/180 👍👍 Bhaiya Mid Term Aagaye the Issliye Lectures Pending the Ab Cover Ho jayenge 👍
@manish_kumar_iitg2 ай бұрын
Solution for BT to DLL :- // track a node that will be accesible for every node to point for left pointer nad prev will point to this root // node, that's it done void solve(Node *root,Node * &head,Node *&prev){ if(!root) return; solve(root->left,head,prev); if(prev == NULL){ head = root; root->left = prev; prev = root; }else{ prev->right = root; root->left = prev; prev = root; } solve(root->right,head,prev); } Node* bToDLL(Node* root) { // code here Node *head = NULL,*prev = NULL; solve(root,head,prev); return head; }
@R.Hrevolution19988 ай бұрын
maza aa gya..
@Aniruddha_Mukherjee8 ай бұрын
Post Order Traversal ( Morris Traversal ) ---------------------------------------------------------------- vector postOrder(Node* root) { // code here vector ans; while(root!=NULL) { /* Agar root ke right null ho to ans ko note down karke , sidha root ke left me jana hoga. */ if(root->right==NULL) { ans.push_back(root->data); root = root->left; } // Agar root ka right subtree exist karein to right subtree me pehle vist karna hoga. else { // agar root ke right exist karein , to root ke right subtree ka left most node pe jana hoga, kyunki wohi se bapas root pe aneka rasta milega. Node *node = root->right; while(node->left!=NULL && node->left!=root) node = node->left; //left most node ka left pointer agar null ho , to first time traverse kar rahe hain. aur is algorithm me (Root , RIGHT , LEFT) rule follow ho raha hain isiliye first time vist karte hi node ko ans array me push karna hoga , and root ko (root = root->right) me firse bhejna hoga yehi kam firse karne ke liye. if(node->left==NULL) { ans.push_back(root->data); node->left = root; root = root->right; } //agar node ke left pointer root ko point karein to samajhna hoga ki hum traverse kar chuke hain , //for that reason humko left subtree traverse karna hoga and node ke left ko firse null karna hoga jo pehle tha. else { node->left = NULL; root = root->left; } } } //Normally PostOrder Traversal ka rule (LEFT , RIGHT , ROOT) hain. // iss algorithm mein (ROOT , RIGHT , LEFT) follow ho rha hain. iss liye reverse karna hoga. reverse(ans.begin(),ans.end()); return ans; } _________________________________________________