LeetCode Symmetric Tree Solution Explained - Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 2024
  • The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
    Join my free exclusive community built to empower programmers! - www.skool.com/...
    Preparing For Your Coding Interviews? Use These Resources
    --------------------
    (My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
    AlgoCademy - algocademy.com...
    Daily Coding Interview Questions - bit.ly/3xw1Sqz
    10% Off Of The Best Web Hosting! - hostinger.com/...
    Follow Me on X/Twitter - x.com/nickwhit...
    Follow My Instagram - / nickwwhite
    Other Social Media
    ----------------------------------------------
    Discord - / discord
    Twitch - / nickwhitettv
    TikTok - / nickwhitetiktok
    LinkedIn - / nicholas-w-white
    Show Support
    ------------------------------------------------------------------------------
    Patreon - / nick_white
    PayPal - paypal.me/nick....
    Become A Member - / @nickwhite
    #coding #programming #softwareengineering
  • บันเทิง

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

  • @djmilen4o
    @djmilen4o 4 ปีที่แล้ว +45

    Dude you are so underrated ... I can't believe TH-cam is doing it to you! Your explanations are way much better than the rest of TH-camrs

    • @nishantsharma3100
      @nishantsharma3100 4 ปีที่แล้ว +6

      have you even watched kevin naughton jr?

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

      @@nishantsharma3100 can't compare

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

      @@nishantsharma3100 I agree Kevin Naughton explanation was better for this question.
      I respect both Kevin and Nick for the amazing teaching content. Some videos are better here and some are better at Kevin's. But I sure do watch both their videos for any solution. They are just amazing. Kudos!!

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

    I am a big fan of your videos! Every time I have a doubt, I check out your videos! In fact, before my interview preparations, every time I am bored and don't want to code, I binge watch your videos.

  • @oscaropdyou
    @oscaropdyou 4 ปีที่แล้ว +17

    Are we passing isMirror(root, root) to check if root is null? We could have started with isMirror(root.left, root.right).

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

    Amazing! I spent about 10 hours to solve this problem, you solved it in 4 minutes. Thanks!

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

    you are literally a gift from heaven. God bless you 😂

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

    Accepted: You have explained better than 99% of the youtubers

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

    Good job. Thanks Nick. I am learning quite a lot from your channel

  • @NeerajSharma-oz1mm
    @NeerajSharma-oz1mm 2 ปีที่แล้ว

    Iterative solution if anyone interested:
    class Solution {
    public:
    bool isSymmetric(TreeNode *root) {
    queue store;
    store.push({root, root});
    while (!store.empty()) {
    pair pr = store.front();
    store.pop();
    if(pr.first && pr.second){
    if(pr.first->val != pr.second->val) return false;
    store.push({pr.first->left, pr.second->right});
    store.push({pr.first->right, pr.second->left});
    } else if(pr.first || pr.second){
    return false;
    }
    }
    return true;
    }
    };

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

    dude excellent!!!!.....i solved it with bfs....but this one was amazing

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

    Leaving a comment bellow because I do like the explanation.

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

    Excellent, Thank you.

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

    In last condition only one isMirror is good enough right?

  • @user-bl4wm4fz6r
    @user-bl4wm4fz6r ปีที่แล้ว

    I had a similar solution, but I added an extra check to see if the left and right val are not equal so that if they are not, it saves on doing the recursive step
    class Solution {
    public boolean isSymmetric(TreeNode root) {
    if(root.left == null && root.right == null) return true;
    return isSymmetric(root.left, root.right);
    }
    private boolean isSymmetric(TreeNode left, TreeNode right){
    if(left == null && right == null) return true;
    if(left == null || right == null ) return false;

    boolean sym = left.val == right.val;
    if(!sym) return false;
    boolean leftSym = isSymmetric(left.left, right.right);
    boolean rightSym = isSymmetric(left.right, right.left);
    return sym && leftSym && rightSym;
    }
    }

    • @energy-tunes
      @energy-tunes 2 หลายเดือนก่อน

      and is short circuit operator it won't check for anything else and needlessly recurse if the vals aren't equal

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

    Great explanation!

  • @mthatipamula
    @mthatipamula 4 ปีที่แล้ว

    Nice solution.

  • @AryanSingh-zv5ch
    @AryanSingh-zv5ch ปีที่แล้ว

    Thanks ❤

  • @techbarikcom
    @techbarikcom 4 ปีที่แล้ว

    Thanks boy!

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

    Nick you are not dum

  • @062supratimbhattacharjee6
    @062supratimbhattacharjee6 3 ปีที่แล้ว

    class Solution {
    public:
    bool checkMirror(TreeNode* root1, TreeNode* root2)
    {
    if(root1==nullptr && root2==nullptr)
    return true;
    if(root1==nullptr || root2==nullptr)
    return false;
    bool isLeftMirror=checkMirror(root1->left,root2->right);
    if(!isLeftMirror)
    return false;
    bool isRightMirror=checkMirror(root1->right,root2->left);
    if(!isRightMirror)
    return false;
    return isLeftMirror && isRightMirror && root1->val==root2->val;
    }
    bool isSymmetric(TreeNode* root) {
    if(root==nullptr)
    return true;
    if(root->left==nullptr && root->right==nullptr)
    return true;
    if(root->left==nullptr || root->right==nullptr)
    return false;
    return checkMirror(root->left,root->right);
    }
    };

  • @2412Anand
    @2412Anand 3 ปีที่แล้ว

    Hey Nick, your video thumbnail is hiding the code

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

      public boolean isSymmetric(TreeNode root) {
      return copyMirror(root, root);
      }
      public boolean copyMirror(TreeNode t1, TreeNode t2) {
      if (t1 == null && t2 == null)
      return true;
      if (t1 == null || t2 == null)
      return false;
      //we are recursivelly call the left tand right trees

      return(t1.val == t2.val) &&
      copyMirror(t1.left, t2.right) &&
      copyMirror(t1.right, t2.left); //right and left
      }
      }

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

    ### C++ ###
    class Solution {
    public:
    bool isSymmetric(TreeNode* root) {
    return root == NULL || isSymmetricHelp(root -> left, root -> right);
    }
    bool isSymmetricHelp(TreeNode* left, TreeNode* right){
    if(left == NULL || right == NULL) return left == right;
    if(left -> val != right -> val) return false;
    return isSymmetricHelp(left -> left, right -> right) &&

    isSymmetricHelp(left -> right, right -> left);
    }
    };

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

    legend

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

    public class Solution {
    public bool IsSymmetric(TreeNode root) {
    return isMirror(root, root);
    }
    public bool isMirror(TreeNode t1, TreeNode t2)
    {
    if (t1 == null && t2 == null) return true;
    if (t1 == null || t2 == null) return false;
    return t1.val == t2.val && isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left);
    }
    }

  • @bhaskarbhasku2921
    @bhaskarbhasku2921 4 ปีที่แล้ว

    You can do bfs also right?

    • @cedriccarteron2578
      @cedriccarteron2578 4 ปีที่แล้ว

      A BFS on two copies of the tree just like in this video ? Oone goes left to Right, The other goes right to left ?

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

    hahahhaha, 0:40

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

    U r not dumb bro 😂