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

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 พ.ย. 2024

ความคิดเห็น • 198

  • @CoderArmy9
    @CoderArmy9  8 หลายเดือนก่อน +77

    Morris Traversal Mein Maja aaya fr💯

  • @kiarakapoor8103
    @kiarakapoor8103 3 หลายเดือนก่อน +16

    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.

  • @itshirdeshk
    @itshirdeshk 8 หลายเดือนก่อน +18

    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;
    }

  • @ManojSinghRawat-x2w
    @ManojSinghRawat-x2w 7 หลายเดือนก่อน +5

    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 😊

  • @ankushladani496
    @ankushladani496 8 หลายเดือนก่อน +6

    Pehle mene pseudocode likha and badme code implement Kiya.
    Post Order Code Done ✅

  • @sakshamsharma3156
    @sakshamsharma3156 4 หลายเดือนก่อน +2

    Literally the best tutorial of Morris Traversal on TH-cam . Thanks sir!

  • @itshirdeshk
    @itshirdeshk 8 หลายเดือนก่อน +6

    Day 161 ✅🔥
    Learning++ bhaiya ✨

  • @sharadjishukla3297
    @sharadjishukla3297 4 หลายเดือนก่อน +1

    // 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;
    }

  • @anupammishra2891
    @anupammishra2891 8 หลายเดือนก่อน

    pura chamak gya bhaiya morris traversal pura ache se best explaination...........🤗🤗🤗🤗🤗

  • @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 2 หลายเดือนก่อน +2

    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;
    }

  • @ramdeoyadav8369
    @ramdeoyadav8369 2 หลายเดือนก่อน +1

    maza a gya video dhekh ke learning first time morris traversal

  • @RohitYadav-rf1oi
    @RohitYadav-rf1oi 28 วันที่ผ่านมา

    //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;
    }
    };

  • @harshvardhanrathore7751
    @harshvardhanrathore7751 2 หลายเดือนก่อน +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;
    }
    };

  • @Okayboy678
    @Okayboy678 8 หลายเดือนก่อน +4

    Thank you for this playlist but just one question is it gonna be a complete one i mean complete DSA using C++?

  • @chirag_mehra5
    @chirag_mehra5 4 หลายเดือนก่อน +1

    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;

  • @devendrasharma8750
    @devendrasharma8750 24 วันที่ผ่านมา

    bhaiya easily understood and chamak gaya sab, thank you bhayia

  • @AnoopRajpoot-i3j
    @AnoopRajpoot-i3j 25 วันที่ผ่านมา

    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

  • @ankushladani496
    @ankushladani496 8 หลายเดือนก่อน +2

    Done Bhaiya... Day 161/180 ✅💯

  • @abhijeetbasfore6816
    @abhijeetbasfore6816 8 หลายเดือนก่อน +3

    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;
    }
    };

  • @codedByAyush
    @codedByAyush หลายเดือนก่อน

    Best explanation on Morris Traversal ❤

  • @Sahilsharma-sk5vr
    @Sahilsharma-sk5vr 3 หลายเดือนก่อน +1

    watching your channelf or the first time and i am really impressed.

  • @rrr9848
    @rrr9848 7 หลายเดือนก่อน +1

    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;
    }
    };

  • @lajpalarjun
    @lajpalarjun 8 หลายเดือนก่อน +1

    Very well explained. Enjoyed the video a lot. Thank you so much.

  • @vaishnavigoswami5707
    @vaishnavigoswami5707 4 หลายเดือนก่อน +1

    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;
    }
    };

  • @skasfarali5314
    @skasfarali5314 2 หลายเดือนก่อน

    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;
    }

  • @nitinverma1902
    @nitinverma1902 7 หลายเดือนก่อน

    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;
    }
    };

  • @ritwiksaha2009
    @ritwiksaha2009 5 หลายเดือนก่อน

    /*
    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;
    }

  • @AKASHKUMAR-li7li
    @AKASHKUMAR-li7li 4 หลายเดือนก่อน

    Best explanation of morris traversal you ever find♥ learning++

  • @mohamedameen9117
    @mohamedameen9117 2 หลายเดือนก่อน

    Thank you so much bhaiya, your explanations are very easy to understand ❤

  • @rohitbisht1107
    @rohitbisht1107 8 หลายเดือนก่อน +1

    Nice bhaiya ❤
    Keep growing ..
    Full support from uttarakhand ❤❤

  • @tradun.s9760
    @tradun.s9760 4 หลายเดือนก่อน

    best lectures on allover youtube

  • @lenovox1carbon664
    @lenovox1carbon664 หลายเดือนก่อน

    All other youtubers simply ignoring morris postorder without any reason except for u 🙏🙏

  • @ankushladani496
    @ankushladani496 8 หลายเดือนก่อน +4

    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 😅😂

  • @heetpatel3037
    @heetpatel3037 5 หลายเดือนก่อน +1

    Jai Shri Ram ❤️
    Thank you Bhaiya 👍🏻
    Ek dum op content

  • @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;

  • @asad_iliyas
    @asad_iliyas 6 หลายเดือนก่อน +1

    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;
    }

  • @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 หลายเดือนก่อน

    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;
    }
    };

  • @zebra-er6xc
    @zebra-er6xc 6 หลายเดือนก่อน

    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;
    }
    };

  • @relaxingtime2411
    @relaxingtime2411 8 หลายเดือนก่อน +1

    Haan Ji Mere Aaka Good Morning ❤

  • @ramsiyaram10082
    @ramsiyaram10082 4 หลายเดือนก่อน

    thanks bhaiya for providing such incredible content

  • @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;
    }
    };

  • @vg_85
    @vg_85 8 หลายเดือนก่อน +1

    Best explanation on internet
    # learning++

  • @SurajGupta-ux2se
    @SurajGupta-ux2se 2 หลายเดือนก่อน

    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;
    }++

  • @bharatmalik2756
    @bharatmalik2756 8 หลายเดือนก่อน +3

    Rohit Bhaiya teaching dsa >> any paid course 😎

  • @AnkitTiwari-o1s
    @AnkitTiwari-o1s 7 หลายเดือนก่อน

    {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}

  • @bidyasagarmohapatra4014
    @bidyasagarmohapatra4014 5 หลายเดือนก่อน

    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;
    }
    };

  • @Prabhu_ShreeRam1
    @Prabhu_ShreeRam1 6 หลายเดือนก่อน

    {
    Learning ++:
    Concepts = Concepts+1;
    }

  • @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;

  • @mr._devil8702
    @mr._devil8702 3 หลายเดือนก่อน

    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;

  • @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;
    }
    };

  • @manish_kumar_iitg
    @manish_kumar_iitg 2 หลายเดือนก่อน

    38:20 Morris Traversal Pseudo cod for in-Order for revision.

  • @dashstar33
    @dashstar33 8 หลายเดือนก่อน +3

    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 หลายเดือนก่อน +3

      Good luck for exam bhai

    • @dashstar33
      @dashstar33 8 หลายเดือนก่อน +1

      ​​@@CoderArmy9Acha Gaya bhai❤❤ abi do bache Hain par firbhi may aajse lecture continue karoonga🤞

  • @kkjhajaisitola2124
    @kkjhajaisitola2124 หลายเดือนก่อน

    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;
    }
    }

  • @mohit6215
    @mohit6215 8 หลายเดือนก่อน +1

    Bhaiya minimum time to burn a tree???

  • @SportsopibyTB
    @SportsopibyTB 8 หลายเดือนก่อน

    Rohit bhaiya
    What will be your upcoming course??

  • @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;
    }
    };

  • @thinker-o5p
    @thinker-o5p 3 หลายเดือนก่อน

    ekdam chamak gaya bhaiya

  • @tesla2146
    @tesla2146 3 หลายเดือนก่อน

    wonderful explanation

  • @ShreeRamSingh-vl9tl
    @ShreeRamSingh-vl9tl 4 หลายเดือนก่อน

    //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;

  • @rrr9848
    @rrr9848 7 หลายเดือนก่อน +1

    Learning++ bhaiya 🎉

    • @CoderArmy9
      @CoderArmy9  7 หลายเดือนก่อน +2

      Keep Going

  • @shivanshgoel2504
    @shivanshgoel2504 3 หลายเดือนก่อน

    *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;
    }
    };

  • @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;
    }

  • @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;
    }
    };

  • @GauravKumar-wk9tr
    @GauravKumar-wk9tr 3 หลายเดือนก่อน

    why is it causing problem if we don't remove link as we have already traversed the left subtree and curr has been move to right subtree then it is obvious that we will never visit left subtree then why it is giving runtime error

  • @shubhamkumarjha9192
    @shubhamkumarjha9192 8 หลายเดือนก่อน +1

    Good morning bhaiya ji. 💖

  • @hemantpatil4575
    @hemantpatil4575 8 หลายเดือนก่อน +1

    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 วันที่ผ่านมา

    Chamak gyaa bhejii❤❤❤

  • @amybross
    @amybross 3 หลายเดือนก่อน

    bhot badhiya bhaiya

  • @kuldeepparmar9861
    @kuldeepparmar9861 8 หลายเดือนก่อน +1

    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;

  • @apna_talks
    @apna_talks หลายเดือนก่อน

    End tak

  • @easyprogramming9074
    @easyprogramming9074 3 หลายเดือนก่อน

    Preorder Morris Traversal k each left ko right and each right ko left m convert krne se we get --- NODE--RIGHT-LEFT . Later we reverse the answer list and we get LEFT---RIGHT----NODE
    Our PostOrder Morris Traversal.

  • @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;
    }
    };

  • @DelightfulTreeSwing-qo6tm
    @DelightfulTreeSwing-qo6tm 8 หลายเดือนก่อน +2

    Haaan bhaiya chamak gya

  • @ahsankhan-rt2kh
    @ahsankhan-rt2kh 8 หลายเดือนก่อน

    Good morning bhaiya.

  • @deep_singh01
    @deep_singh01 8 หลายเดือนก่อน

    Day 161/180 👍👍
    Bhaiya Mid Term Aagaye the Issliye Lectures Pending the Ab Cover Ho jayenge 👍

  • @pranavmittal9619
    @pranavmittal9619 8 หลายเดือนก่อน +1

    hukum aapka hukm sar aakho per

  • @YashSaini007
    @YashSaini007 8 หลายเดือนก่อน +1

    Day 161/180 #180DaysOfCode

  • @YashSaini007
    @YashSaini007 8 หลายเดือนก่อน +1

    Bhaiya Radhe Radhe 🙏

    • @CoderArmy9
      @CoderArmy9  8 หลายเดือนก่อน +1

      Radhe Radhe bhai

  • @whatthefactandfiction
    @whatthefactandfiction 26 วันที่ผ่านมา

    Impressive

  • @YASHYADAV-f9i
    @YASHYADAV-f9i หลายเดือนก่อน

    bhaiya learning++

  • @ManishSingh-m1m
    @ManishSingh-m1m 3 หลายเดือนก่อน

    chamak gya bhaiya

  • @You-ul7dr
    @You-ul7dr 2 หลายเดือนก่อน

    learning ++ thanks

  • @Code_with_Tiwarijii
    @Code_with_Tiwarijii 8 หลายเดือนก่อน +1

    Chamak gya bhaiya

  • @ankushladani496
    @ankushladani496 8 หลายเดือนก่อน

    Thanks Bhaiya...❤🎉

  • @Ajeetkumar-uo4hf
    @Ajeetkumar-uo4hf 15 วันที่ผ่านมา

    Like kar diya bhai

  • @ankushladani496
    @ankushladani496 8 หลายเดือนก่อน +1

    Learning++

  • @Eng_wallah
    @Eng_wallah 8 หลายเดือนก่อน

    Bhaiya radhe radhe 🙏🏻🙏🏻

  • @vijaysingh-f8w1c
    @vijaysingh-f8w1c 9 วันที่ผ่านมา

    Name Vijay singh NEXUS and DSA both i will do and get the placement in future ❤

  • @Code_with_Tiwarijii
    @Code_with_Tiwarijii 8 หลายเดือนก่อน

    Learning++ bhaiya

  • @puneetsetia6800
    @puneetsetia6800 8 หลายเดือนก่อน

    Bhaut badiya

  • @allinonemoviesyt
    @allinonemoviesyt 8 หลายเดือนก่อน

    good morning bhaiya ji

  • @binauralbeats-relaxingmusi4336
    @binauralbeats-relaxingmusi4336 7 หลายเดือนก่อน

    Dsa notes me page no 35 me.
    Num=0;
    Cout

  • @pratimakamath5985
    @pratimakamath5985 8 หลายเดือนก่อน

    Bhaiyya..agar 180 days ho gaye...toh aap DSA bandh kar denge kya ?? Kyuki bohot contents baaki hai

  • @nandanks2068
    @nandanks2068 หลายเดือนก่อน

    Learning ++ 😉

  • @R.Hrevolution1998
    @R.Hrevolution1998 8 หลายเดือนก่อน

    maza aa gya..

  • @swarnadeepsaha4455
    @swarnadeepsaha4455 8 หลายเดือนก่อน

    Bhaiya best of best❤

  • @nidhiraj9686
    @nidhiraj9686 7 หลายเดือนก่อน +1

    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;
    }

  • @a71patiladitya45
    @a71patiladitya45 14 วันที่ผ่านมา

    learning++ ❣❣