Test If A Binary Tree Is Symmetric ("Symmetric Tree" on Leetcode)

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ก.ย. 2024
  • Code - backtobackswe....
    Free 5-Day Mini-Course: backtobackswe.com
    Try Our Full Platform: backtobackswe....
    📹 Intuitive Video Explanations
    🏃 Run Code As You Learn
    💾 Save Progress
    ❓New Unseen Questions
    🔎 Get All Solutions
    Question: Write a program to check if a binary tree is symmetric in structure as well as value.
    A binary tree is symmetric if we can draw a vertical line down from the root and the left and right subtrees are mirrors of each other in structure and value.
    The least this could be is O(n) time because we have to inspect all n nodes values to ensure that they conform to the tree being symmetric.
    All we need to do is traverse the tree correctly with our recursion checking pairs for conformity.
    The Conditions
    If the root is null, the tree is symmetric.
    Our checking function will take 2 nodes in to compare for symmetry.
    Our Base Cases
    If both nodes passed in are null, by default we have a symmetric tree.
    If one node is null and the other is not, then we have incongruence, the tree is not symmetric since a pair failed.
    If both nodes are null, then compare values. If values are equal, then good. Go left and go right.
    How do we come up with this on the spot?
    First recognize the base cases.
    Then recognize we will need some way to compare pairs of nodes since that's what matters when checking for symmetry.
    Even if this were an array that is how we would do it, pairs of indices.
    Then recognize the way the recursive case must continue the work on the way down and your solution would look like this at the end.
    Complexities
    Time: O(n)
    Space: O(h) (as we discussed, it can't really be O(n) worst case since
    the first check would fail if the tree is skewed. The worst case IS O(h))
    ++++++++++++++++++++++++++++++++++++++++++++++++++
    HackerRank: / @hackerrankofficial
    Tuschar Roy: / tusharroy2525
    GeeksForGeeks: / @geeksforgeeksvideos
    Jarvis Johnson: / vsympathyv
    Success In Tech: / @successintech
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++
    This problem is problem 10.2 in the book Elements of Programming Interviews by Adnan Aziz

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

  • @BackToBackSWE
    @BackToBackSWE  5 ปีที่แล้ว +7

    Table of Contents:
    Video Setup 0:00 - 0:18
    Problem Introduction 0:18 - 2:45
    Find The Pattern With An Array 2:45 - 4:20
    Find The Pattern With A Binary Tree 4:20 - 6:48
    The Code. 6:48 - 9:17
    Time & Space Complexity 9:17 - 10:58
    Apply This To Other Recursive Problems 10:58 - 12:05
    As with every video, I'm not perfect, I hope I didn't make any gross mistakes in the video. If all is well, great, this is the explanation for how to test if a binary tree is symmetric.

  • @yashshreeshinde4394
    @yashshreeshinde4394 4 ปีที่แล้ว +34

    Please don't feel sorry for being loud,that's your speciality when you talk ;
    it feels like a professional teacher is teaching and he wants all his students to pass in any cost.

    • @BackToBackSWE
      @BackToBackSWE  4 ปีที่แล้ว +10

      lol ok, old videos had bad gear and no mic, and yeah I gave my life for 1.5 years to make these videos I want everyone to pass at any cost lol

  • @soumyadeeproy6611
    @soumyadeeproy6611 4 ปีที่แล้ว +7

    The energy in your explanation is just awesome,it makes everyone to listen your words with great concern,it is really true that positive person give off positive energy

  • @frankchen9264
    @frankchen9264 5 ปีที่แล้ว +9

    Awesome video! I think you decouple this problem quite elegantly. Trying to solve a problem in a recursive way is not easy. You did a great job!

  • @shtephl354
    @shtephl354 4 ปีที่แล้ว +13

    Damn, if my interviewer had your energy in this video, i'd be half pumped / half having an anxiety attack. But anyways, I'm on a binge on your videos, thanks for the help!

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

      yeah, old video, sorry.

    • @parthpawar7837
      @parthpawar7837 4 ปีที่แล้ว +5

      I love this energy! Keeps me interested, stay pumped up

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

      @@parthpawar7837 ye

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

    The best explanation. Even the bubbling up guys 😆 like nick white and kevin fades in front of you! Increased my confidence dude. Who the hack disliked!! Complete disguise!

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

      And l Liked my own comment ✌️

  • @LilyEvans1996
    @LilyEvans1996 5 ปีที่แล้ว +15

    duddeeeeeee! thankssssss!!!! I needed you in my life and I didn't even know until now

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

    lmao, "sustain our lives" i felt that

  • @ruchikadakalia8972
    @ruchikadakalia8972 4 ปีที่แล้ว +5

    That was a fantastic explanation!!...It very well shows how passionate you are about your work...and believe me you can make people love data-stuctures..

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

    Thankss Dudeee!!! finally, after this video i solved this problem successfully.

  • @dephc0n1
    @dephc0n1 5 ปีที่แล้ว +5

    Your editing has improved a ton! Keep up the good work.

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

    Watching on X-mas day after 2 years :)

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

    Your video is so good. The explanation is super clear.

  • @vivekojha4807
    @vivekojha4807 4 ปีที่แล้ว +5

    Woah your energy boosted me as well. Thanks!

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

    U deserve more likes and subscribers.

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

      I know, eventually

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

      @@BackToBackSWE I guess after watching your video, every one rushes to leetcode to solve that instead of a like😂😂

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

    very nice, you are the best youtuber who can explain leetcode problems clearly and make them easy to understand.

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

    4 years later, still great

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

    You are so brilliant !! Thanks for making these videos.

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

    thanks sir for the simplest explanation i have came across so far

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

    Salute you dude, hands down please make more problems about the graph theory, you are mindblowing

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

    I studied at UMD. I am proud of you man!

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว

      Let's put turtles on the map 🐢🐢

  • @Kgp-ty5dk
    @Kgp-ty5dk 4 ปีที่แล้ว +2

    Thanks for the explanation boss. The space complexity part was great as well Call stack etc. Can you make a quick video on Min depth of binary tree. Algthough I understand it. But its a bit tricky!

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

    like your passion dude

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

    I think the space complexity is also O(n). The worst case is basically a symmetric list with the middle as the tree root. O(n/2) ≈ O(n), in terms of space for the stack frames

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

    Awesome !!!! You have really good teaching skills.

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

    Thanks for this video I really like the way you explain this problem

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

    Could I use an in order traversal to push all the values into a vector, then do a palindrome test in the vector?

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว

      how so?

    • @prabhjyotsingh139
      @prabhjyotsingh139 5 ปีที่แล้ว

      Yes you can do that way, but before applying Inorder traversal convert the tree into complete binary tree.

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

      @@prabhjyotsingh139 oh yeah I get it now, yeah you can do it like that as long as you make the provision for null nodes

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

      Yes you can, the problem is that the time complexity will be O(n) in every case. The recursive solution is O(n) in worst case.

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

    Thank you! I've been watching MIT lectures on data structures and algorithms and when I don't understand something, I always get a super clear explanation here! You're terrific at explaining things

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

      Thank You!!
      Do check out backtobackswe.com/platform/content
      and please recommend us to your family and friends :)

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

    Super thanks!

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

    Best explanation, hands down 👌

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

    You’re just a legend!

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

    Nice explanation! Thanks for the video!

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

    The energy is infectious 🚀

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

    good as always :)
    thanks!

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

    I have an interview in 2 days. If only I had seen your videos earlier :'(

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

    Loved it

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

    you are great man !!! thanks

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

    7:47 nice rhyming symmetry

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

    Fantastic videos, man! Thank you so much for your work! P.S.: It's very sad that you don't have many comments/views, but most importantly you're delivering the quality, so all these things will come soon! Awesome!

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว +4

      You have no idea how much that means to me! :)
      Every day I wonder if it is worth it to continue doing this. I have like 300 questions I want to cover (that I did while learning) and I realize that is a HUGE year-long project at the best if I keep it up.
      Normally videos have this breakdown:
      1 hr notes
      1 hr shooting
      2-6 hours editing
      30 minutes to publish
      Not sure how I can keep it up during school but we will see (it starts Jan. 28th)
      So yeah I think they are ok but holy crap they take a long time. I have a long story behind why I do these videos, but I have like an internal thing keeping me going despite knowing that literally no one watches right now.
      Thank you so much, I really appreciate this.

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

      @@BackToBackSWE Keep doing them. These vids are the best. Listen if I get a the job I want from these vids I'm donating you 500 bucks!

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว

      @@qaipak1 haha ok

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

      @@BackToBackSWE please continue if you can and have the desire! You are amazing!

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว

      @@LilyEvans1996 workin' on it

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

    This is great, except you missed 5:13 in the table of contents! :)

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

    Hey man you are so great ! haha

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

    Brilliant

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

    Please make more videos. why have you stopped making videos? Your channel is great bro.

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

      I'm working at Twitter and don't make public videos since we run backtobackswe.com

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

    Bro please upload more video on trees. I am waiting for that.

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

    Love it mannn !!
    just one comment, try to decrease the energy just a little/
    Thank you so much !!

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

    Thank you!

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

    You are just amazing !!!!!!

  • @rds6129
    @rds6129 5 ปีที่แล้ว +4

    pause at 6:43 and you can see the solution. Otherwise you're standing in front of it the whole time dude.

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว +4

      sorry, this is an old video

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

      @@BackToBackSWE still dope for an old vid

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

    Thank you, I did inorder traversal of the tree and then did palindrone . It passed 187 cases but failed 188 th case.

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

      nice!

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

      Same here.

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

      @@Kev1nTheCoder can you tell at which test case is it failing ???

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

      @@neeravganate9562 Sorry I don't remember. I checked the Leetcode but it turns out they changed their test cases.

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

      @@Kev1nTheCoder np got it now

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

    Thank you, Sir, this helped

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

    This was really helpful. Thanks a lot.
    Can you please make a video on Diameter of Binary Tree? It's really confusing and I am stuck on it. It would be really helpful if you could do that. Thanks

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

    Thanksssss!!!

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

    funny and great explanation ) thanks a Lot ))

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

    so good!

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

    But how to do it iteratively?

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว

      uh, I'd need to refresh myself on this problem

  • @lawrenberghanson4401
    @lawrenberghanson4401 5 ปีที่แล้ว +4

    LOL, your endings. You can just say "thanks and see you next time" but I guess if you did that your endings wouldn't be special.

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

    One day you will know how to end your videos. We have faith in you.

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

    Lovely video bruh. May the $$$ be with you.

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

      idc about $$$ only ok, ik that it may seem that way but I just need to fulfill my vision. And thx

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

    You're the one

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

      Thank you, glad you liked it 😀
      Do check out backtobackswe.com/platform/content
      and please recommend us to your family and friends 😀

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

    awesome :)

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

    At first i thought he was Lewis Hamilton :D

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

    You were really yelling in the early videos 😆

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

      Yeah I was, things improve over time if you just stay with them. A life lesson. We get better everyday.

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

    Oh lord I might as well have a heart attack right now : D

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

      what

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

      @@BackToBackSWE Still teaching in the same library room now?
      Also, do you have any tips on how to remember the code for problems you've done, thanks!

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

    RIP to the people who disliked this

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

    Why are you standing in front of the code 99% of the time?

  • @khan.mansoor
    @khan.mansoor 3 ปีที่แล้ว

    You're moving way too much and it's a bit distracting otherwise you're a wonderful instructor.

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

    Very nice!!