Construct Binary Tree From Preorder And Inorder Traversal-(Microsoft, Amazon):Live Coding 🧑🏻‍💻

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

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

  • @AIsinger96
    @AIsinger96 6 หลายเดือนก่อน +10

    most underrated channel, I'll definitely post about this channel on linkedin.

    • @codestorywithMIK
      @codestorywithMIK  6 หลายเดือนก่อน +5

      This means a lot ❤️❤️🙏🙏
      Feel free to tag me on LinkedIn 😇

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

      @@codestorywithMIK yes bhaiya

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

    Bhai kya hi simple samjhate ho. Sab samajh aa jata hai.

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

    Different approach using 2 indices, one you mentioned in inorder & postorder one!
    class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
    return buildTree(preorder, inorder, 0, inorder.length-1, 0, preorder.length-1);
    }
    public TreeNode buildTree(int[] preorder, int[] inorder, int inStart, int inEnd, int preStart, int preEnd) {
    if(inStart > inEnd || preStart > preEnd) return null;
    TreeNode root = new TreeNode(preorder[preStart]); // preorder = [3,2,1,4,5], root-> 3 (root, left, right)
    int i = inStart;
    for(; i

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

      Wonderful.
      So glad to see other approaches ❤️

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

    SLIGHTLY OPTIMIZED APPROACH USING MAP
    TreeNode* solve(vector& preorder, vector& inorder, int start, int end, int &idx,map &inList ) {
    if(start>end){
    return NULL;
    }
    int rootVal = preorder[idx];
    int inorderIdx = inList[rootVal];
    idx++;
    TreeNode* root = new TreeNode(rootVal);
    root->left = solve(preorder, inorder, start, inorderIdx-1, idx,inList);
    root->right = solve(preorder, inorder, inorderIdx+1, end, idx,inList);
    return root;
    }
    TreeNode* buildTree(vector& preorder, vector& inorder) {
    int idx = 0;
    int n = preorder.size();
    map inList;
    for(int i=0;i

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

      Yeah using map Time-Complexity Will reduce to O(n) instead of O(n^2)

  • @JJ-tp2dd
    @JJ-tp2dd ปีที่แล้ว +5

    Java Implementation:
    class Solution {

    private int idx;

    private TreeNode solve(int[] preorder, int[] inorder, int start, int end) {

    if(start > end) {
    return null;
    }

    TreeNode root = new TreeNode(preorder[idx]);
    int i = start;

    for(; i

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

      My below Java code is not passing example test cases
      I compared line by line . It looks same . What is wrong below
      class Solution {
      public TreeNode buildTree(int[] preorder, int[] inorder) {
      int idx = 0;
      return helper(preorder, inorder, 0, preorder.length - 1, idx);
      }
      TreeNode helper(int[] preorder, int[] inorder, int start, int end, int idx) {
      if (start > end)
      return null;
      int rootValue = preorder[idx];
      int i = start;
      for (; i

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

      @@surajsidar2294 keep your variable "idx" out of the function.

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

    thanks a lot bhaiya.... sirf story se code likh liya... ;)

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

    in the optimized version, no need to pass the inorder arry; we already have a map
    TreeNode* construct(vector& preorder, int l, int r, int& idx) {

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

    bhaiya pls recursion padha do na us playlist me bus leetcode ke que sol krna plssssssssssss

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

    great explanation...

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

    Beautiful question and great explanation!!

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

    Super bhaiyya

  • @aditgulia
    @aditgulia 6 หลายเดือนก่อน +2

    bal baccha karna hoga was epic line hahaha... btw nice explaination

  • @basujain8928
    @basujain8928 4 หลายเดือนก่อน +3

    guys is wale ques vese striver soln jyada samajh aya mujhe

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

      Really glad to know you understood the solution , be it from any resource 😇❤️
      And definitely I would be glad if you can share improvements or feedbacks.

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

    Amazing man, thanks again.

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

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

    hey, i used the approach from ur latest video of construction of binary tree using postorder and inorder and wrote this but its giving incorrect output:
    class Solution {
    public:
    TreeNode* solve(vector& inorder, vector& preorder, int inStart, int inEnd, int preStart, int preEnd, map& inMap) {
    if (preStart > preEnd || inStart > inEnd) return NULL;
    TreeNode* root = new TreeNode(preorder[preStart]);
    int i = inMap[root->val];
    int leftSize = i - inStart;
    root->left = solve(inorder, preorder, inStart, i - 1, preStart + 1, preStart + leftSize, inMap);
    root->right = solve(inorder, preorder, i + 1, inEnd, preStart + leftSize + 1, preEnd, inMap);
    return root;
    }
    TreeNode* buildTree(vector& inorder, vector& preorder) {
    int n = inorder.size();
    int inStart = 0, inEnd = n - 1;
    int preStart = 0, preEnd = n - 1;
    map inMap;
    for (int i = 0; i < n; i++) inMap[inorder[i]] = i;
    TreeNode* root = solve(inorder, preorder, inStart, inEnd, preStart, preEnd, inMap);
    return root;
    }
    };
    can u pls tell me where i'm wrong? even chatgpt can't figure it out

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

      i figured out the error, it was a really silly mistake

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

    Why we need to pass the idx as pass by reference? Can someone please explain this once??

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

      There is need of pass by refernce.
      If you write same code in Java it won't work.
      Unlike C++ there is no pass by reference in Java , so there is need to create global variable idx.
      Below was my code in Java which didn't work
      class Solution {
      //int idx;
      public TreeNode buildTree(int[] preorder, int[] inorder) {
      int idx = 0;
      return helper(preorder, inorder, 0, preorder.length - 1,idx);
      }
      TreeNode helper(int[] preorder, int[] inorder, int start, int end,int idx) {
      if (start > end)
      return null;
      int rootValue = preorder[idx];
      int i = start;
      for (; i

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

      @@surajsidar2294 Even mine is not working, have you find the solution for the same.

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

      If you don't pass it by reference, by default it'll be passed by value i.e each the recursive function gets called it'll be reset. By passing by reference you are able to remember the previous idx value.