Build Tree Postorder and Inorder | C++ Placement Course | Lecture 27.4

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • Complete C++ Placement Course (Data Structures+Algorithm) : • C++ Full Course | C++...
    Telegram: t.me/apnikaksh...
    Instagram: / dhattarwalaman
    Notes of this Lecture:

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

  • @chamangupta4624
    @chamangupta4624 3 ปีที่แล้ว +30

    The reason that right node of current is being built first (than left node ) is that while decrementing from post order array's end we get the right element of the node only , as Postorder follows left right root sequence , so first make right node than left node.

    • @sayonika254
      @sayonika254 2 ปีที่แล้ว

      why did she use 'static' in that line static int index=0 ???

    • @pramantomar822
      @pramantomar822 2 ปีที่แล้ว +4

      @@sayonika254 so that it does not reinitialise itself to 0 again and again during function calls

    • @arunpanwar4152
      @arunpanwar4152 ปีที่แล้ว

      nice mjja aaya

    • @anugoluguruchaitanya1313
      @anugoluguruchaitanya1313 ปีที่แล้ว

      Thank You.

  • @kiranp5828
    @kiranp5828 3 ปีที่แล้ว +43

    This sister was so eloquent and articulate in what she says.
    It has become so much easier to comprehend the concepts of binary tree.
    Contrarily, that guy who appeared in the previous videos was the worst(so hasty in my sense).
    I suggest to make her permanent for teaching the tutorials.

  • @sleepypanda7172
    @sleepypanda7172 3 ปีที่แล้ว +34

    here is the source code for reference purpose:
    #include
    using namespace std;
    class Node
    {
    public:
    int data;
    Node *right;
    Node *left;
    Node(int val)
    {
    data = val;
    right = NULL;
    left = NULL;
    }
    };
    int search(int inorder[], int start, int end, int val)
    {
    for (int i = start; i end)
    {
    return NULL;
    }
    int val = postorder[idx];
    idx--;
    Node *curr = new Node(val);
    if (start == end)
    {
    return curr;
    }
    int pos = search(inorder, start, end, val);
    curr->right = maketree(postorder, inorder, pos+1, end);
    curr->left = maketree(postorder, inorder, start, pos-1);
    return curr;
    }
    void print_inorder(Node *root)
    {
    if (root == NULL)
    {
    return;
    }
    print_inorder(root->left);
    cout data right);
    }
    int main()
    {
    int postorder[] = {4, 2, 5, 3, 1};
    int inorder[] = {4, 2, 1, 5, 3};
    Node *root = maketree(postorder, inorder, 0, 4);
    print_inorder(root);
    return 0;
    }

    • @faizansaqeeb3390
      @faizansaqeeb3390 3 ปีที่แล้ว +1

      #include
      using namespace std;
      class Node
      {
      public:
      int data;
      Node* left;
      Node* right;
      Node(int val)
      {
      data=val;
      left=NULL;
      right=NULL;
      }
      };
      int search(int inorder[],int start,int end,int val)
      {
      for (int i = start; i end)
      {
      /* code */
      return NULL;
      }
      int val=postorder[idx];
      idx--;
      Node* curr = new Node(val);
      if (start==end)
      {
      /* code */
      return curr;
      }
      int pos = search(inorder,start,end,val);
      curr->right=buildtree(postorder,inorder,pos+1,end);
      curr->left=buildtree(postorder,inorder,start,pos-1);
      return curr;
      }
      void inorderPrint(Node* root)
      {
      if (root==NULL)
      {
      /* code */
      return;
      }
      inorderPrint(root->left);
      cout

    • @faizansaqeeb3390
      @faizansaqeeb3390 3 ปีที่แล้ว +1

      i have a error pls solve it

    • @sleepypanda7172
      @sleepypanda7172 3 ปีที่แล้ว +1

      @@faizansaqeeb3390 what error?

    • @faizansaqeeb3390
      @faizansaqeeb3390 3 ปีที่แล้ว +1

      @@sleepypanda7172 it is executed.. But not showing output

    • @sleepypanda7172
      @sleepypanda7172 3 ปีที่แล้ว +1

      @@faizansaqeeb3390 paste your code here

  • @rankitgujjar80
    @rankitgujjar80 3 ปีที่แล้ว +3

    code for reference
    #include
    using namespace std;
    struct Node{
    int data;
    struct Node* left;
    struct Node* right;
    Node(int val){
    data=val;
    left=NULL;
    right=NULL;
    }
    };
    void printPostorder(struct Node* node){
    if(node==NULL)return;
    printPostorder(node->left);
    printPostorder(node->right);
    coutleft->right=new Node(5);
    root->right->left=new Node(6);
    root->right->right=new Node(7);
    int postorder[]={4,5,2,6,7,3,1};
    int inorder[]={4,2,5,1,6,3,7};
    struct Node* node=buildTree( postorder, inorder, 0, 6);
    cout

  • @chamangupta4624
    @chamangupta4624 3 ปีที่แล้ว +3

    actualy the start==end is not required , it will automatically be staisfied by base condn

  • @anuragpandey3341
    @anuragpandey3341 3 ปีที่แล้ว +6

    8:55 Just write end instead of 4. We are passing end in buildTree

    • @ritikingle3188
      @ritikingle3188 3 ปีที่แล้ว +1

      end pointer lies only in inorder not in postorder .

    • @shivanibanjara5118
      @shivanibanjara5118 2 ปีที่แล้ว

      We are initializing a static variable so used 4

  • @sleepypanda7172
    @sleepypanda7172 3 ปีที่แล้ว +13

    isn't there is mistake in first 30 sec? she said post order definition wrong or am I interpreting it wrong?
    can be tiny human error though. Btw explanation was great!! absolutely loving the course

  • @ishaangarg8292
    @ishaangarg8292 3 ปีที่แล้ว +3

    Thankyou bhaiya and team AB TO PHODENGE

  • @ankitshrestha6481
    @ankitshrestha6481 3 ปีที่แล้ว +6

    13:05 Why order is changed, rule of Postorder is (Left, Right, Root)
    but here we first travel right side and then left in recursive??
    Anyone know??

  • @reetpriye8807
    @reetpriye8807 3 ปีที่แล้ว +7

    00:15 Slight mistake. Postorder definition.

    • @sleepypanda7172
      @sleepypanda7172 3 ปีที่แล้ว +2

      haa na mujhe bhi laga. Tiny human error

  • @imskr2683
    @imskr2683 2 ปีที่แล้ว +2

    Remember C++ Guys...
    Pass postorder & inorder by reference in your function call, otherwise there may be TLE because of several copies creation of post and inorder sequence in successive recursive calls.

  • @vivekmathur3068
    @vivekmathur3068 3 ปีที่แล้ว +8

    I literally learnt this in college rn.

    • @shivamverma3777
      @shivamverma3777 3 ปีที่แล้ว

      Which year

    • @vivekmathur3068
      @vivekmathur3068 3 ปีที่แล้ว

      @@shivamverma3777 Year 1

    • @apoorvo12
      @apoorvo12 3 ปีที่แล้ว

      @@vivekmathur3068 C++ in 1st year in college ?

    • @aryanagarwal2071
      @aryanagarwal2071 3 ปีที่แล้ว

      They didnt teach us the algo and code though

    • @vivekmathur3068
      @vivekmathur3068 3 ปีที่แล้ว +1

      @@apoorvo12 I am not in a college that follows the IIT curricula. We don't spend the first year doing general studies.

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

    leetcode 106 /**
    * 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:
    unordered_mapin;
    public:
    TreeNode* helper(vector& inorder, vector& postorder,int start,int end,int &index){
    if(start>end) return nullptr;
    int curr=postorder[index--];
    TreeNode* ele=new TreeNode(curr);
    int pos=in[curr];
    if(start==end) return ele;
    ele->right=helper(inorder,postorder,pos+1,end,index);
    ele->left=helper(inorder,postorder,start,pos-1,index);
    return ele;
    }
    TreeNode* buildTree(vector& inorder, vector& postorder) {
    int index=inorder.size()-1;
    for(int i=0;i

  • @hardiknegi7066
    @hardiknegi7066 3 ปีที่แล้ว +11

    It will be more easier to comprehend if u upload notes.

    • @AlokSingh-jw8fr
      @AlokSingh-jw8fr 3 ปีที่แล้ว

      #include
      //#include"Binarytree.h"
      #include
      using namespace std;
      int Search(int inorder[],int l,int e,int cur)
      {
      for(int i=l;iinorderE)
      return NULL;
      idx--;
      BinaryTreeNode*root=new BinaryTreeNode(cur);
      if(inorderS==inorderE)
      return root;
      int pos=Search(Inorder,inorderS,inorderE,cur);
      root->right=buildTree(Postorder,Inorder,pos+1,inorderE);
      root->left=buildTree(Postorder,Inorder,inorderS,pos-1);
      return root;
      }
      void print(BinaryTreeNode*root)
      {
      if(root==NULL)
      return ;

      queue pendingnodes;
      pendingnodes.push(root);
      while(!pendingnodes.empty())
      {

      BinaryTreeNode* front=pendingnodes.front();
      cout

    • @AlokSingh-jw8fr
      @AlokSingh-jw8fr 3 ปีที่แล้ว

      Here,is the code for reference purpose.
      I have additionally created a funtion to print the original tree in levelwise manner

    • @hardiknegi7066
      @hardiknegi7066 3 ปีที่แล้ว

      @@AlokSingh-jw8fr thanks mate :)

    • @AlokSingh-jw8fr
      @AlokSingh-jw8fr 3 ปีที่แล้ว

      @@hardiknegi7066 Don't forget to change the name of class as per your class name

  • @dheerajsaraswat227
    @dheerajsaraswat227 3 ปีที่แล้ว

    in this approach searching are complete in O(n^2) but it will be optimize by using unordered set or hash table this course is only for your understanding not for gurrented placement course

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

    amazing

  • @everyone8057
    @everyone8057 ปีที่แล้ว

    very nice teacher!

  • @rajansingh1919
    @rajansingh1919 ปีที่แล้ว

    4:21. plz correction postorder tree mein sbse phle left subtree ko build krte hai.

  • @tanishapandey9931
    @tanishapandey9931 3 ปีที่แล้ว +2

    #include
    using namespace std;
    struct Node
    {
    int data;
    Node* left;
    Node* right;
    Node(int val)
    {
    data = val;
    left = NULL;
    right = NULL;
    }
    };
    void inorder(Node* root)
    {
    if (root == NULL)
    {
    return;
    }
    inorder(root->left);
    cout right = buildTree(ino, post, pos + 1, end);
    curr->left = buildTree(ino, post, start, pos - 1);
    return curr;
    }
    int main()
    {

    int post[] = {4,2,5,3,1};
    int ino[] = {4,2,1,5,3};
    Node* root = buildTree(ino, post, 0, 4);
    inorder(root);
    return 0;
    }
    Can someone tell me why am I getting overflow exception at Node* curr = new Node(val);

    • @rheotv1859
      @rheotv1859 3 ปีที่แล้ว

      Ur struct Node is not correct ,you have to write struct Node* left ;
      And
      struct Node* right ;
      while defining the pointers..

  • @shikhamaurya4453
    @shikhamaurya4453 3 ปีที่แล้ว +1

    All I can do
    Is to say "Shukriyaa"

  • @harshpanwar1550
    @harshpanwar1550 2 ปีที่แล้ว

    Thanks a lot🔥🔥

  • @sarthaktyagii7683
    @sarthaktyagii7683 3 ปีที่แล้ว

    nice work guys

  • @vishalprajapati1208
    @vishalprajapati1208 3 ปีที่แล้ว

    thank you so much

  • @Patrick_08
    @Patrick_08 3 ปีที่แล้ว

    15:56 -> 16:15
    Full aur complete konsa statement correct hai bhaiyya.
    Please consider any reply

  • @KapilSharma-ow7xe
    @KapilSharma-ow7xe 11 หลายเดือนก่อน

    in build tree function why we take idx as 4

  • @paragbansal3
    @paragbansal3 ปีที่แล้ว

    7:46

  • @vaibhavsharma4500
    @vaibhavsharma4500 3 ปีที่แล้ว

    Thank you 🙏🙏

    • @Gunjan2000
      @Gunjan2000 3 ปีที่แล้ว

      th-cam.com/video/enbdzJhjPgE/w-d-xo.html !

  • @riseofvarunff1021
    @riseofvarunff1021 ปีที่แล้ว

    the above code is for a fixed size ,what should we do for passing different cases

  • @AarushPuri-sy2qk
    @AarushPuri-sy2qk ปีที่แล้ว

    not able to print the output, whenever im trying to print blank screen appears

  • @adityaraj1284
    @adityaraj1284 3 ปีที่แล้ว

    Didi mujhe tree data structures k algorithm approach m bhot dikkat aa rha h 😭 maine ye playlist dekha mujhe smjh bhi aaya lkin jb mai trees.k different questions bnata hu toh logical approach nhi kr paa rhe

    • @sleepypanda7172
      @sleepypanda7172 3 ปีที่แล้ว

      watch aditya verma's video. He made a simple common approach for all BT question. & yeah take a moment to practice basic recusrion otherwise you won't be able to solve it you are weak in recursion

  • @nithinshukla4679
    @nithinshukla4679 2 ปีที่แล้ว

    i was so confused till 5:19 cuz of the postorder, thinking whatever i learnt till now was useless. Shouldve put a message in the video saying that there was an error in the sequence

    • @NormieCyrox
      @NormieCyrox 2 ปีที่แล้ว

      yes i am also confused .....can u please explain it

    • @nithinshukla4679
      @nithinshukla4679 2 ปีที่แล้ว

      @@NormieCyrox I recommend you search for another video on this topic bro

  • @shaibazbadai2309
    @shaibazbadai2309 ปีที่แล้ว

    One doubt:
    In line 32: created an object not a pointer, then how come in line 38 & 39 we are using '->' not dot operator?

  • @tashidhondup6502
    @tashidhondup6502 3 ปีที่แล้ว +2

    Premiere📍

  • @prashantsingh514
    @prashantsingh514 3 ปีที่แล้ว +4

    Fun fact:- if run this recursion without base condition , it would still run properly...

    • @sadikalikaradiya
      @sadikalikaradiya 3 ปีที่แล้ว

      No, it is not working. If you remove start> end condition.

  • @vishvrana3799
    @vishvrana3799 3 ปีที่แล้ว +1

    //program to construct tree using postorder and inorder
    #include
    using namespace std;
    class node{
    public:
    int data;
    node*left;
    node*right;
    };
    node* newNode(int data)
    {
    node *Node=new node();
    Node->data=data;
    Node->left=NULL;
    Node->right=NULL;
    return(Node);
    }
    int search(int in[],int start,int end,int value)
    {
    for(int i=start;iend)
    {
    return NULL;
    }
    node* tnode=newNode(post[postindex]);
    postindex--;
    /*int val=post[postindex];
    postindex--;
    node *tnode=newNode(val);*/
    if(start==end)
    {
    return(tnode);
    }
    int inIndex=search(in,start,end,tnode->data);
    tnode->right=buildtree(post,in,inIndex+1,end);
    tnode->left=buildtree(post,in,start,inIndex-1);
    return(tnode);
    }
    void preorder(node *root)
    {
    if(root==NULL)
    {
    return;
    }
    else{
    cout

  • @gauravpawar9179
    @gauravpawar9179 3 ปีที่แล้ว +1

    Notes kb upload honge

  • @shubhangiherode514
    @shubhangiherode514 ปีที่แล้ว

    Where are the notes of this lecture?

  • @sayonika254
    @sayonika254 2 ปีที่แล้ว

    why did she use 'static' in that line static int index=0 ???

  • @bharatchakrawarti9602
    @bharatchakrawarti9602 3 ปีที่แล้ว

    Thanku Soo Much Bhaiya Ji and Faculties Members ;) :)

  • @akshatvyas2594
    @akshatvyas2594 3 ปีที่แล้ว +2

    can't we initialize idx by end. 8:52

    • @shashankshetti9769
      @shashankshetti9769 3 ปีที่แล้ว

      Yes , its possible ig

    • @HARSHAGRAWALBCE-nb3gb
      @HARSHAGRAWALBCE-nb3gb 3 ปีที่แล้ว

      no because we call start and end recursively also then we changes values by pos+1 or pos-1;

    • @gauravupreti9340
      @gauravupreti9340 3 ปีที่แล้ว

      We can do that also.

    • @gauravupreti9340
      @gauravupreti9340 3 ปีที่แล้ว

      @@HARSHAGRAWALBCE-nb3gb idx variable is static, it initializes only one time i.e. in the first recursive call.

    • @HARSHAGRAWALBCE-nb3gb
      @HARSHAGRAWALBCE-nb3gb 3 ปีที่แล้ว

      @@gauravupreti9340 yes

  • @ce003abhimanyu9
    @ce003abhimanyu9 ปีที่แล้ว

    static ka use kyu h
    uske bina error kyu de rha h code
    can anybody tell
    @Apna College

    • @neelabhkaushik5514
      @neelabhkaushik5514 ปีที่แล้ว

      taki hr bar function dubara call krne pr wapis se 0 na ho jae

  • @kartikgoyal359
    @kartikgoyal359 3 ปีที่แล้ว +1

    what is complete binary tree ??

    • @bohotsaaridhop
      @bohotsaaridhop 3 ปีที่แล้ว +1

      Jismein sabhi elements ke child elements 2 ho except last level

  • @aishiksarkar8226
    @aishiksarkar8226 2 ปีที่แล้ว

    Didi at 0:32 u made a mistake in defining Postorder
    Plz Edit It

  • @shreyashachoudhary480
    @shreyashachoudhary480 3 ปีที่แล้ว

    root->message = "Thanks Apni Team"

  • @arzitmahajan7703
    @arzitmahajan7703 3 ปีที่แล้ว +1

    What are the first 8 lines of this code can anyone help me?

    • @TripleAYT
      @TripleAYT 3 ปีที่แล้ว

      It's is construction of Node structure

    • @arzitmahajan7703
      @arzitmahajan7703 3 ปีที่แล้ว

      @@TripleAYT thanks man i got it

    • @swapnilsrivastava9407
      @swapnilsrivastava9407 3 ปีที่แล้ว

      Company direct ask you write algorithm of this code then you write the step how a code start and end...

  • @atin_jagotra
    @atin_jagotra 3 ปีที่แล้ว

    static int idx = 4 . But why. don't we have to put its value acc. to its size?

    • @parthmittal5625
      @parthmittal5625 3 ปีที่แล้ว +2

      Yeah, static int idx = end; will work.

    • @atin_jagotra
      @atin_jagotra 3 ปีที่แล้ว

      @@parthmittal5625 thanks

    • @anaskaratela7171
      @anaskaratela7171 3 ปีที่แล้ว +1

      Apparently it is n-1

    • @anaskaratela7171
      @anaskaratela7171 3 ปีที่แล้ว +1

      Bro can u plz help me i am getting the reversed inorder sequence in output

    • @HARSHAGRAWALBCE-nb3gb
      @HARSHAGRAWALBCE-nb3gb 3 ปีที่แล้ว

      @@parthmittal5625 though it does not work as star and end changes when we call recursively end =pos-1 or pos+1 for left and right subtree thats why it deos not help hope you get my point

  • @anchalmishra2322
    @anchalmishra2322 3 ปีที่แล้ว

    at 4:24 why postorder sequence has changed ????

    • @gauravgianchandani
      @gauravgianchandani 3 ปีที่แล้ว

      It's just a graphical mistake, the order will remain as it was before

  • @sakshamsengar9798
    @sakshamsengar9798 3 ปีที่แล้ว

    aapki awaaz boht pyari hai

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

    so much confusing explanation
    😵‍💫😵‍💫😵‍💫😵‍💫😵‍💫

  • @harpic949
    @harpic949 3 ปีที่แล้ว

    Ma panner khaunga

  • @ShubhamKumar-ot4bc
    @ShubhamKumar-ot4bc 3 ปีที่แล้ว

    animation se acha board pe btate to jada smjh me ata

    • @sleepypanda7172
      @sleepypanda7172 3 ปีที่แล้ว

      kahi aur ja ke padh le
      animation se bhi teko samjh nahi aa rha to kya he bole ab, maha Dumb hai tu fir
      bhaiyya is giving best content seekhna aana chahiye . pravachan ki tarah dekho ge to toh seekh liye fir

  • @Rj_Cricket_lover
    @Rj_Cricket_lover 3 ปีที่แล้ว

    Bhai java ? Bhai piz

  • @ujjwalmahajan7581
    @ujjwalmahajan7581 3 ปีที่แล้ว +2

    Bhaiya yeh C++ ke lectures baad mai dalna pehle please organic ke lectures daldo and modern physics ke notes bhi upload karo
    Please bhaiya bahut tension hai muje papers ki
    Sirf aap hi sahara ho mera
    Please bhaiya, help me
    Mai apse request karta hun

  • @anmolchokhani6362
    @anmolchokhani6362 2 ปีที่แล้ว

    #include
    using namespace std;
    class Node
    {
    public:
    int data;
    Node* left;
    Node* right;
    Node(int val)
    {
    data=val;
    left=NULL;
    right=NULL;
    }
    };
    int search(int inorder[],int start,int end,int curr)
    {
    for(int i=start;iend)
    return NULL;
    static int indx=6;
    int curr= postorder[indx];
    indx--;
    Node* node=new Node(curr);
    if(start==end)
    return node;
    int inorderpos= search(inorder,start,end,curr);
    node->right=buildTree(postorder,inorder,inorderpos+1,end);
    node->left= buildTree(postorder,inorder,start,inorderpos-1);

    return node;
    }
    void inordertraversal(Node* root)
    {
    if(root==NULL)
    return;
    inordertraversal(root->left);
    cout