Morris Traversal : Inorder Traversal | Flatten Binary Tree to LinkedList | Post Order | PreOrder

  Рет қаралды 13,248

Coder Army

Coder Army

Күн бұрын

Пікірлер: 201
@CoderArmy9
@CoderArmy9 8 ай бұрын
Morris Traversal Mein Maja aaya fr💯
@siddheshpandey7905
@siddheshpandey7905 7 ай бұрын
bhut maja aaya
@divyanshrawat2859
@divyanshrawat2859 4 ай бұрын
@Sanataniblood-mb5pu
@Sanataniblood-mb5pu 4 ай бұрын
❤❤❤❤
@kiarakapoor8103
@kiarakapoor8103 3 ай бұрын
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.
@lucifersamrat6280
@lucifersamrat6280 3 ай бұрын
exactly same situation
@mohamedameen9117
@mohamedameen9117 3 ай бұрын
same here
@itshirdeshk
@itshirdeshk 8 ай бұрын
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; }
@pranavmittal9619
@pranavmittal9619 8 ай бұрын
thanks bro
@sakshamsharma3156
@sakshamsharma3156 5 ай бұрын
Literally the best tutorial of Morris Traversal on KZbin . Thanks sir!
@ankushladani496
@ankushladani496 8 ай бұрын
Pehle mene pseudocode likha and badme code implement Kiya. Post Order Code Done ✅
@anupammishra2891
@anupammishra2891 8 ай бұрын
pura chamak gya bhaiya morris traversal pura ache se best explaination...........🤗🤗🤗🤗🤗
@itshirdeshk
@itshirdeshk 8 ай бұрын
Day 161 ✅🔥 Learning++ bhaiya ✨
@jagmaggarg6901
@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; }
@sharadjishukla3297
@sharadjishukla3297 4 ай бұрын
// 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
@devendrasharma8750 Ай бұрын
bhaiya easily understood and chamak gaya sab, thank you bhayia
@ManojSinghRawat-x2w
@ManojSinghRawat-x2w 8 ай бұрын
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 😊
@codedByAyush
@codedByAyush 2 ай бұрын
Best explanation on Morris Traversal ❤
@ramdeoyadav8369
@ramdeoyadav8369 3 ай бұрын
maza a gya video dhekh ke learning first time morris traversal
@developer999
@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;
@xlr8-Ujjwal
@xlr8-Ujjwal 3 ай бұрын
Post Order Traversal: vector postOrder(Node* node) { vector ans; while(node) { // Right part doesn't exist if(!node ->right){ ans.push_back(node ->data); node = node ->left; } else { // Right part exist Node *curr = node ->right; while(curr ->left && curr ->left != node){ curr = curr ->left; } //Right subtree is not traversed yet if(curr ->left == NULL) { curr ->left = node; ans.push_back(node ->data); node = node ->right; } else { //Right subtree already traversed curr ->left = NULL; node = node ->left; } } } reverse(ans.begin(), ans.end()); return ans; }
@RohitYadav-rf1oi
@RohitYadav-rf1oi Ай бұрын
//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-sk5vr
@Sahilsharma-sk5vr 3 ай бұрын
watching your channelf or the first time and i am really impressed.
@chirag_mehra5
@chirag_mehra5 4 ай бұрын
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;
@ankushladani496
@ankushladani496 8 ай бұрын
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 😅😂
@ankushladani496
@ankushladani496 8 ай бұрын
Done Bhaiya... Day 161/180 ✅💯
@AnoopRajpoot-i3j
@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
@lajpalarjun
@lajpalarjun 8 ай бұрын
Very well explained. Enjoyed the video a lot. Thank you so much.
@rrr9848
@rrr9848 8 ай бұрын
Learning++ bhaiya 🎉
@CoderArmy9
@CoderArmy9 8 ай бұрын
Keep Going
@AKASHKUMAR-li7li
@AKASHKUMAR-li7li 5 ай бұрын
Best explanation of morris traversal you ever find♥ learning++
@mohamedameen9117
@mohamedameen9117 3 ай бұрын
Thank you so much bhaiya, your explanations are very easy to understand ❤
@heetpatel3037
@heetpatel3037 5 ай бұрын
Jai Shri Ram ❤️ Thank you Bhaiya 👍🏻 Ek dum op content
@vaishnavigoswami5707
@vaishnavigoswami5707 5 ай бұрын
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.s9760
@tradun.s9760 5 ай бұрын
best lectures on allover youtube
@harshvardhanrathore7751
@harshvardhanrathore7751 2 ай бұрын
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; } };
@nitinverma1902
@nitinverma1902 8 ай бұрын
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; } };
@rohitbisht1107
@rohitbisht1107 8 ай бұрын
Nice bhaiya ❤ Keep growing .. Full support from uttarakhand ❤❤
@abhijeetbasfore6816
@abhijeetbasfore6816 8 ай бұрын
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
@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
@ramsiyaram10082
@ramsiyaram10082 4 ай бұрын
thanks bhaiya for providing such incredible content
@RJFF3-r7w
@RJFF3-r7w 12 күн бұрын
Chamak gaya bhaiya.... Learning ++;
@SurajGupta-ux2se
@SurajGupta-ux2se 3 ай бұрын
learning++ vector postorderMorris(Node* root){ vector ans; Node* curr = root; while(curr){ if(curr->right){ Node* temp = curr->right; while(temp->left && temp->left != curr) temp = temp->left; if(!temp->left){ ans.push_back(curr->data); temp->left = curr; curr = curr->right; }else if(temp->left == curr){ temp->left = NULL; curr = curr->left; } }else{ ans.push_back(curr->data); curr = curr->left; } } reverse(ans.begin(), ans.end()); return ans; }++
@lenovox1carbon664
@lenovox1carbon664 2 ай бұрын
All other youtubers simply ignoring morris postorder without any reason except for u 🙏🙏
@skasfarali5314
@skasfarali5314 3 ай бұрын
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; }
@bharatmalik2756
@bharatmalik2756 8 ай бұрын
Rohit Bhaiya teaching dsa >> any paid course 😎
@relaxingtime2411
@relaxingtime2411 8 ай бұрын
Haan Ji Mere Aaka Good Morning ❤
@vg_85
@vg_85 8 ай бұрын
Best explanation on internet # learning++
@ritwiksaha2009
@ritwiksaha2009 6 ай бұрын
/* 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; }
@RanjeetKumarceb
@RanjeetKumarceb 8 ай бұрын
// 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; } };
@ramsiyaram10082
@ramsiyaram10082 4 ай бұрын
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_iliyas
@asad_iliyas 7 ай бұрын
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; }
@kartikbohra1904
@kartikbohra1904 7 ай бұрын
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-er6xc
@zebra-er6xc 7 ай бұрын
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; } };
@rrr9848
@rrr9848 8 ай бұрын
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-o1s
@AnkitTiwari-o1s 8 ай бұрын
{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}
@tesla2146
@tesla2146 4 ай бұрын
wonderful explanation
@thinker-o5p
@thinker-o5p 4 ай бұрын
ekdam chamak gaya bhaiya
@bidyasagarmohapatra4014
@bidyasagarmohapatra4014 6 ай бұрын
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; } };
@shubhamkumarjha9192
@shubhamkumarjha9192 8 ай бұрын
Good morning bhaiya ji. 💖
@shivanshgoel2504
@shivanshgoel2504 4 ай бұрын
*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_ShreeRam1
@Prabhu_ShreeRam1 6 ай бұрын
{ Learning ++: Concepts = Concepts+1; }
@mr._devil8702
@mr._devil8702 4 ай бұрын
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-fg2gz
@RishabhChatterjee-fg2gz 4 ай бұрын
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;
@kumarashutosh-p3n
@kumarashutosh-p3n 8 ай бұрын
class Solution{ public: vector preOrder(Node* root) { vectorans; while(root){ if(!root->left){ ans.push_back(root->data); root=root->right; } else{ Node* curr=root->left; while(curr->right && curr->right!=root){ curr=curr->right; } if(curr->right==NULL){ ans.push_back(root->data); curr->right=root; root=root->left; } else{ curr->right=NULL; root=root->right; } } } return ans; } };
@Pallab01
@Pallab01 8 ай бұрын
❤‍🔥 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; } };
@kkjhajaisitola2124
@kkjhajaisitola2124 2 ай бұрын
void flatten(Node *root){ while(root){ if(root->left){ Node *curr=root->left; while(curr->right) curr=curr->right; curr->right=root->right; root->right=root->left; root->left=NULL; } root=root->right; } }
@dashstar33
@dashstar33 8 ай бұрын
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❤❤
@CoderArmy9
@CoderArmy9 8 ай бұрын
Good luck for exam bhai
@dashstar33
@dashstar33 8 ай бұрын
​​@@CoderArmy9Acha Gaya bhai❤❤ abi do bache Hain par firbhi may aajse lecture continue karoonga🤞
@Okayboy678
@Okayboy678 8 ай бұрын
Thank you for this playlist but just one question is it gonna be a complete one i mean complete DSA using C++?
@manish_kumar_iitg
@manish_kumar_iitg 2 ай бұрын
38:20 Morris Traversal Pseudo cod for in-Order for revision.
@ShreeRamSingh-vl9tl
@ShreeRamSingh-vl9tl 5 ай бұрын
//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;
@amybross
@amybross 4 ай бұрын
bhot badhiya bhaiya
@anupammishra6514
@anupammishra6514 8 ай бұрын
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-ku8ez
@OK-ku8ez 7 ай бұрын
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; }
@risingsoul3514
@risingsoul3514 3 ай бұрын
postorder solution -- class Solution{ public: vector postOrder(Node* node) { // code here vectorans; while(node){ if(!node->right){ ans.push_back(node->data); node=node->left; }else { Node *curr=node->right; while(curr->left&&curr->left!=node){ curr=curr->left; } if(curr->left==NULL){ ans.push_back(node->data); curr->left=node; node=node->right; }else { curr->left=NULL; node=node->left; } } }reverse(ans.begin(),ans.end()); return ans; } };
@RJFF3-r7w
@RJFF3-r7w 12 күн бұрын
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; }
@hemantpatil4575
@hemantpatil4575 8 ай бұрын
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; } };
@amannegi1817
@amannegi1817 19 күн бұрын
Chamak gyaa bhejii❤❤❤
@ahsankhan-rt2kh
@ahsankhan-rt2kh 8 ай бұрын
Good morning bhaiya.
@YashSaini007
@YashSaini007 8 ай бұрын
Bhaiya Radhe Radhe 🙏
@CoderArmy9
@CoderArmy9 8 ай бұрын
Radhe Radhe bhai
@DelightfulTreeSwing-qo6tm
@DelightfulTreeSwing-qo6tm 8 ай бұрын
Haaan bhaiya chamak gya
@Code_with_Tiwarijii
@Code_with_Tiwarijii 8 ай бұрын
Chamak gya bhaiya
@nidhiraj9686
@nidhiraj9686 7 ай бұрын
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_wallah
@Eng_wallah 8 ай бұрын
Bhaiya radhe radhe 🙏🏻🙏🏻
@pranavmittal9619
@pranavmittal9619 8 ай бұрын
hukum aapka hukm sar aakho per
@swarnadeepsaha4455
@swarnadeepsaha4455 8 ай бұрын
Bhaiya best of best❤
@Ajeetkumar-uo4hf
@Ajeetkumar-uo4hf Ай бұрын
Like kar diya bhai
@ankushladani496
@ankushladani496 8 ай бұрын
Thanks Bhaiya...❤🎉
@YASHYADAV-f9i
@YASHYADAV-f9i Ай бұрын
bhaiya learning++
@ManishSingh-m1m
@ManishSingh-m1m 3 ай бұрын
chamak gya bhaiya
@kuldeepparmar9861
@kuldeepparmar9861 8 ай бұрын
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;
@allinonemoviesyt
@allinonemoviesyt 8 ай бұрын
good morning bhaiya ji
@mahmudaliza4079
@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; } }
@whatthefactandfiction
@whatthefactandfiction Ай бұрын
Impressive
@ankushladani496
@ankushladani496 8 ай бұрын
Learning++
@puneetsetia6800
@puneetsetia6800 8 ай бұрын
Bhaut badiya
@akashkumawat-xk1eg
@akashkumawat-xk1eg 2 ай бұрын
vector postorderTraversal(TreeNode* root) { TreeNode* curr,*prev; vectorans; prev=root; while(prev){ if(!prev->right){ ans.push_back(prev->val); prev=prev->left; } else{ curr=prev->right; while(curr->left&&curr->left!=prev){ curr=curr->left; } if(!curr->left){ curr->left=prev; ans.push_back(prev->val); prev=prev->right; } else{ curr->left=NULL; prev=prev->left; } } } reverse(ans.begin(),ans.end()); return ans; } moj karo😉😉
@mohit6215
@mohit6215 8 ай бұрын
Bhaiya minimum time to burn a tree???
@You-ul7dr
@You-ul7dr 3 ай бұрын
learning ++ thanks
@apna_talks
@apna_talks Ай бұрын
End tak
@datarobo1718
@datarobo1718 5 ай бұрын
vector postorderTraversal(TreeNode* root) { vectorans; 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==nullptr){ ans.push_back(root->val); temp->left=root; root=root->right; } else{ temp->left=nullptr; root=root->left; } } } reverse(ans.begin(), ans.end()); return ans; }
@YashSaini007
@YashSaini007 8 ай бұрын
Day 161/180 #180DaysOfCode
@Code_with_Tiwarijii
@Code_with_Tiwarijii 8 ай бұрын
Learning++ bhaiya
@deep_singh01
@deep_singh01 8 ай бұрын
Day 161/180 👍👍 Bhaiya Mid Term Aagaye the Issliye Lectures Pending the Ab Cover Ho jayenge 👍
@manish_kumar_iitg
@manish_kumar_iitg 2 ай бұрын
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.Hrevolution1998
@R.Hrevolution1998 8 ай бұрын
maza aa gya..
@Aniruddha_Mukherjee
@Aniruddha_Mukherjee 8 ай бұрын
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; } _________________________________________________
Burning Tree || Maximum Path Sum between 2 Special Nodes
1:27:12
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 21 МЛН
Чистка воды совком от денег
00:32
FD Vasya
Рет қаралды 2,4 МЛН
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 13 МЛН
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 117 МЛН
Morris Inorder Tree Traversal
11:44
Tushar Roy - Coding Made Simple
Рет қаралды 147 М.
Preorder, Inorder , Postorder Traversal (Shortcut Trick) for BINARY TREE
8:13
94. Binary Tree Inorder Traversal | Morris Traversal | Space - O(1)
28:09
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 21 МЛН