Flip Equivalent Binary Trees | Simple Recursion | Dry Run | Leetcode 951 | codestorywithMIK

Поділитися
Вставка
  • Опубліковано 5 лис 2024

КОМЕНТАРІ • 42

  • @literally_ankur
    @literally_ankur 13 днів тому +21

    I just noticed that you are one of our college alumni.
    I really love your content and my leetcode consistency and rating improvement is because of you.
    I hope we can connect some day
    Thanks for making iiest proud ❤️❤️

    • @Zoro_tao
      @Zoro_tao 13 днів тому

      Which college?

    • @literally_ankur
      @literally_ankur 13 днів тому +5

      ​@@Zoro_taoiiest shibpur

    • @codestorywithMIK
      @codestorywithMIK  12 днів тому +3

      I just saw this comment.
      This made my day.
      Thank you so much ♥️🙏😇

  • @yuvrajsoni6865
    @yuvrajsoni6865 13 днів тому +13

    Bhaiya, please make a DSA sheet with important questions for practice. It would be helpful

    • @codestorywithMIK
      @codestorywithMIK  13 днів тому

      Please check the sheet link on my github page.
      I will update them soon ♥️
      github.com/MAZHARMIK

  • @AnshuHarshit
    @AnshuHarshit 11 днів тому

    Bhaiya maine aapka graph concept wali playlist puri dekhi, aur graph samjh me bhi aane laga. Aur abhi recursion wali dekh rha hoon lekin questions nhi ban pa rhe.

  • @nirmalgurjar8181
    @nirmalgurjar8181 12 днів тому +5

    We can use short circuit evaluation so that in case first case return true, 2nd case won't be executed and hence we will able to prune some cases.
    return ((flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right)) || (flipEquiv(root1.right, root2.left) && flipEquiv(root1.left, root2.right)));

  • @manibhushankumarsingh5196
    @manibhushankumarsingh5196 12 днів тому +1

    my approach was exactly same but i was not confident on leap of faith!!

  • @badhrinarayana7
    @badhrinarayana7 12 днів тому +1

    clean code, nice approach
    after first 3 mins , I got the concept and solved my self
    tip:avoid extra recursive calls , maintain a flag to check, whether to flip or not flip.(time , space efficient)
    here's my code , thanks brother
    class Solution {
    private boolean compareSubTrees(TreeNode root1, TreeNode root2,boolean flip)
    {
    if(root1==null && root2==null)return true;
    if(root1==null ||root2==null)return false;
    if(root1.val!=root2.val)return false;
    if(root1.left!=null && root2.left!=null && root1.left.val!=root2.left.val || root1.left!=null && root2.right!=null && root1.left.val==root2.right.val || root1.right!=null && root2.left!=null && root1.right.val==root2.left.val)flip=true;
    if(flip){
    return compareSubTrees(root1.left,root2.right,false) && compareSubTrees(root1.right,root2.left,false);
    }
    return compareSubTrees(root1.left,root2.left,false) && compareSubTrees(root1.right,root2.right,false);
    }
    public boolean flipEquiv(TreeNode root1, TreeNode root2) {
    return compareSubTrees(root1,root2,false);
    }
    }

  • @anushiagrawal
    @anushiagrawal 10 днів тому

    Sir can you please tell how you thought of recursion and wrote the calls?

  • @RohitKumar-dz8dh
    @RohitKumar-dz8dh 12 днів тому +1

    Thanks 😊

  • @ugcwithaddi
    @ugcwithaddi 13 днів тому +1

    Easy today 😊

  • @salmaniproductions1104
    @salmaniproductions1104 13 днів тому +1

    Thank you so much bhaiyya

  • @codeandtalk6
    @codeandtalk6 12 днів тому

    ❤❤❤❤ bro last contest problem 4 . check if dfsstr is palindrome explain bro

  • @HarshSharma-hi9vc
    @HarshSharma-hi9vc 13 днів тому

    badhiya

  • @nish0798
    @nish0798 12 днів тому

    bhaiya can we use inverrt tree and same tree problem to solve this like we invert(root1) and after inverting root1 chck if root1 and root are same

  • @jappreetsingh60
    @jappreetsingh60 13 днів тому

    Can you please upload manacher algo bhaiya...last week contest 420, there was a last question on that algo. Please🙏🏻

  • @Anime-ub7vs
    @Anime-ub7vs 13 днів тому +3

    bool flipEquiv(TreeNode* root1, TreeNode* root2) {
    if(root1 == NULL && root2 == NULL) return true;
    if(root1 == NULL || root2 == NULL) return false;
    if(root1->val != root2->val) return false;
    // Recursively check if flipping the children results in equivalence
    bool noFlip = flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right);
    bool flip = flipEquiv(root1->left, root2->right) && flipEquiv(root1->right, root2->left);
    return noFlip || flip;
    } my code

  • @Anime-ub7vs
    @Anime-ub7vs 13 днів тому

    bhaiya please upload max width of ramp problem app ne kha tha hum iski monotonic stack wali approach discuss karenge

    • @codestorywithMIK
      @codestorywithMIK  13 днів тому +1

      Yes i will try to upload that in weekend when I get free time ♥️

  • @AkOp-bf9vm
    @AkOp-bf9vm 12 днів тому +1

    my code
    class Solution {
    public:
    bool solve(TreeNode* head1, TreeNode* head2){
    if(head1==NULL && head2==NULL) return 1;
    if(head1==NULL || head2==NULL) return 0;
    if(head1->val!=head2->val) return 0;
    if(solve(head1->left,head2->right)==0 && solve(head1->left,head2->left)==0) return 0;
    if(solve(head1->right,head2->right)==0 && solve(head1->right,head2->left)==0) return 0;
    return 1;

    }
    bool flipEquiv(TreeNode* root1, TreeNode* root2) {
    return solve(root1,root2);
    }
    };

  • @anjanasahay2172
    @anjanasahay2172 13 днів тому

    Will Invert Binary Tree and Same Tree Question of Leetcode work here ???
    If I simply call those functions???
    Then can someone help me with the code in cpp calling both these functions??

  • @SurajGupta-gc9tz
    @SurajGupta-gc9tz 12 днів тому +1

    bhaiya ye hai mera code
    class Solution {
    public:
    void addnodes(int parent, TreeNode* node, map& mp) {
    if (node == NULL)
    return;
    addnodes(parent, node->left, mp);
    mp[parent].insert(node->val);
    addnodes(parent, node->right, mp);

    }
    void dfs(TreeNode* node, map& mpleft, map& mpright) {
    if (node == NULL) {
    return;
    }
    dfs(node->left, mpleft, mpright);
    addnodes(node->val, node->left, mpleft);
    addnodes(node->val, node->right, mpright);
    dfs(node->right, mpleft, mpright);
    }
    bool checkValid(TreeNode* node, map& left1, map& right1, map& left2, map& right2) {
    if (node == NULL)
    return true;
    if (checkValid(node->left, left1, right1, left2, right2) == false)
    return false;

    if ((left1[node->val] == left2[node->val] && right1[node->val] == right2[node->val])
    || (left1[node->val] == right2[node->val] && right1[node->val] == left2[node->val])){
    //skip
    }
    else
    return false;
    return checkValid(node->right, left1, right1, left2, right2);

    }
    bool flipEquiv(TreeNode* root1, TreeNode* root2) {
    if (root1 == NULL && root2 == NULL)
    return true;
    if (root1 == NULL && root2 != NULL ||
    root1 != NULL && root2 == NULL)
    return false;
    map left1;
    map right1;
    map left2;
    map right2;
    dfs(root1, left1, right1);
    dfs(root2, left2, right2);
    int sz1 = left1.size() + right1.size(), sz2 = left2.size() + right2.size();
    if (sz1 != sz2)
    return false;
    return checkValid(root1, left1, right1, left2, right2);

    }
    };

  • @Manpreetkaur-xr7cg
    @Manpreetkaur-xr7cg 13 днів тому

    👏👏

  • @DivyaPrakash-bj6zk
    @DivyaPrakash-bj6zk 13 днів тому +1

    Yrr tumhara channel sabka karke ek geniune dikkat ho gayi. Yaar ab jaise abhi 11 baj rhe hai, mai shaam ke side daily question solve krta hu. Ab tumne video upload kardi aur thumbnail mei likha h ki ye question recursion se hoga. Toh ab mujhe pta chal gaya ki ye recursion ka question h toh mai asaan se solve kr sakta hu.
    Baaki questions be it easy medium or hard sabke saath aise hi hota hai. Since I have subscribed you, latest video aa jaati aur I get to know ki iss topic ka question hai. This way meri self thinking nahi hopaati.
    I would suggest you upload by end of the day the answer of today's question. There is no point in uploading it very early. Jo bhi dekhega bhi video toh wo shaam mei dekhega ek baar attempt karke.

    • @DivyaPrakash-bj6zk
      @DivyaPrakash-bj6zk 13 днів тому

      Now unfortunately I would need to unsubscribe and search for your channel everytime for an answer.

    • @codestorywithMIK
      @codestorywithMIK  13 днів тому +6

      My bad.
      I will take care of this next time ♥️

    • @DivyaPrakash-bj6zk
      @DivyaPrakash-bj6zk 13 днів тому +1

      @@codestorywithMIK Thanks brother. You are One of the best on YT tbh 🫱🏼‍🫲🏽

    • @YashMalav-kh1ov
      @YashMalav-kh1ov 13 днів тому +5

      logo ko jeevan mai kaisi kaisi dikkate ho rhi hai😂 tumhe kon bol rha hai sedha youtube kholo phle khud se try kro potd subh 5 bje hi aa jata hai ....

    • @DivyaPrakash-bj6zk
      @DivyaPrakash-bj6zk 13 днів тому

      @@YashMalav-kh1ov bhai UA-cam sirf ye channel thodi dekhne ko kholta hu, aur bhi chize hai. Office mei rehta hu kabhi koi kaam padd gaya ya timepass mei hi khol liya toh UA-cam suggestions mei frequently visited channel ka recent upload dikha deta hai aur pata chal jata h kis topic se hoga daily question. Shaam ko office se aake karne baith ta hu toh uss trah ki learning nahi ho paati kyuki pta hota h kis data structure ya alog se hoga.
      Ab koi sawaal h uspe thumbnail mei likha h Djikstra variation, toh mujhe shaam mei pta hoga ki Djikstra lagegi. Agar topic na likha hota toh mai khud figures out krta pehle ki Djikstra lag sakti hai.
      Pta nahi tumlog ko ye problem kyu nahi aa rahi hai.

  • @PubgMerabeta
    @PubgMerabeta 12 днів тому

    bro binary tree playlist konsi hae ? Can you provide the link ?

    • @codestorywithMIK
      @codestorywithMIK  12 днів тому +1

      ua-cam.com/play/PLpIkg8OmuX-K23LhcamOcDlTBisiNJy5E.html&si=ROszrnxnLF8JtCTJ
      Hope this helps ♥️🙏