Maximum Depth of Binary Tree

Поделиться
HTML-код
  • Опубликовано: 19 сен 2024
  • For business inquiries email partnerships@k2.codes Discord: bit.ly/K2-discord

Комментарии • 137

  • @KevinNaughtonJr
    @KevinNaughtonJr  5 лет назад +160

    THERE IS NOTHING WRONG WITH GOING TO THE MOVIES ALONE

    • @saloneerege2006
      @saloneerege2006 5 лет назад

      I have always wanted to try that and I completely agree with you :-)

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад +2

      @@saloneerege2006 Haha do it!!! This weekend that's your hw :)

    • @jlecampana
      @jlecampana 5 лет назад +2

      I'd rather go alone instead of trying to catch the FlakyFriendException

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад +3

      @@jlecampana The worst kind of exception lol

    • @Salesforce_Nexus
      @Salesforce_Nexus 4 года назад

      you're just awesome mann!!! I like you're style and also remember...you make scene...always :)

  • @MadeByDAY
    @MadeByDAY 5 лет назад +65

    > give narrative/backstory on problem intuition ✅
    > lowkey roasting his subscribers about going to the movies alone ✅
    > literally holds tree during explanation ✅
    > gives time complexity at end ✅
    keep the videos coming, DAILY UPLOADS SOON™

  • @deepakkumarshukla
    @deepakkumarshukla 4 года назад +28

    Dude, this theatre analogy has unraveled the concept of recursion in my mind. Best!

  • @pratyushyuvraj4994
    @pratyushyuvraj4994 3 года назад +7

    you just explain the concepts so flawlessly I didn't have to go through your code just your explanation was good enough to write code myself. Thanks :)

  • @neosapien247
    @neosapien247 4 года назад +14

    This is the best description of recursion on the internet.

  • @minoltandlovu4377
    @minoltandlovu4377 3 года назад +4

    Never thought I would ever understand recursion, you just gave the best explanation. Thank you!

  • @mohammednadeem333
    @mohammednadeem333 4 года назад +9

    I blindly give a like to your videos because I know it will always be awesome prior watching it 🙂. Thank you so much for wonderful channel
    Unlike others, you try to make it simple and best. I encourage others to also give a like because it is deserved!!

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад +3

      Mohammed Nadeem thanks so much Mohammed I really appreciate your support!!!

  • @stevefox7418
    @stevefox7418 4 года назад +4

    The theater analogy is really good for explaining recursion. 👍

  • @hitherto1994
    @hitherto1994 4 года назад +1

    Mind blown at the theater analogy .. i dont think I will ever forget to do tree depth now... thank you

  • @currenluna
    @currenluna 4 года назад +2

    Your explanations are super clear and straight to the point. Thanks for helping me out!!

  • @ambermichellemartinez
    @ambermichellemartinez Год назад

    This explanation, especially the movie theater analogy really made me fully understand. Was watching a bunch of videos til I saw this one, thanks! Always a lifesaver!

  • @lliliiliillliliiliili5180
    @lliliiliillliliiliili5180 5 лет назад +8

    LMAO I love the totally professional vibe and then there's just that random intro mumble rap

    • @thespicycabbage
      @thespicycabbage 3 года назад +1

      i literally thought i had another youtube video/spotify song playing the first few seconds lol

  • @MaulikSakhida
    @MaulikSakhida 5 лет назад +1

    Bit more clarification on Height Vs Depth of tree
    The depth of a node is the number of edges from the node to the tree's root node.
    A root node will have a depth of 0.
    The height of a node is the number of edges on the longest path from the node to a leaf.
    A leaf node will have a height of 0.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад

      Maulik Sakhida thanks for the extra info Maulik definitely good clarification!

  • @Sailgodgreenearth
    @Sailgodgreenearth 2 года назад +1

    nice explanation. This is the first time I am hearing you and I think the theater analogy is best explanation I have heard. (Subscribed you)

  • @TharaMesseroux1
    @TharaMesseroux1 2 года назад

    The Theater example was eye-opening. Thanks, Kevin!

  • @danieltannor6647
    @danieltannor6647 4 года назад +1

    It's important to mention that the +1 happens at each level, it is not only 'us' in the movie theater who're adding +1

  • @Sonyemman97
    @Sonyemman97 2 года назад

    that was the best explanation of recursion I've probably ever heard in my life lol

  • @oxanda7687
    @oxanda7687 4 года назад

    This was such a great explanation. Better than the other top video where the guy just solves it and says "It's this way because... yeah... you need to recursively call the function... it's pretty easy if you don't know it then you should look up more"

  • @pineful4861
    @pineful4861 3 года назад +1

    beautiful explanation. I was really struggling with grasping the recursion conceptually but you're explanation was very helpful.

    • @KevinNaughtonJr
      @KevinNaughtonJr  3 года назад

      Thanks! If you enjoy my explanations i recommend joining a premium plan on the interviewing service I created! thedailybyte.dev/?ref=kevin

  • @hassansmallah
    @hassansmallah 4 года назад +1

    Thank you
    Python:
    class Solution:
    def maxDepth(self, root: TreeNode) -> int:
    if not root:
    return 0
    left = self.maxDepth(root.left)
    right = self.maxDepth(root.right)
    return max(left,right) + 1

    • @jx3751
      @jx3751 4 года назад

      thanks for the python solution

  • @nealpatel7696
    @nealpatel7696 5 лет назад +1

    Wow. I think I saw Stanford using the theatre example to explain recursion. They must've seen your video haha, great work!

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад +1

      hahah thanks Neal! Yeah there's a lot of ways to explain it but whatever makes sense :)

  • @marcoerazo3077
    @marcoerazo3077 Год назад

    Best explanation of recursion I've ever seen!!

  • @sharmilabaskaran7373
    @sharmilabaskaran7373 4 года назад +1

    Thanks ! your movie theatre example will always come into my mind when I solve this ..

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад +1

      Sharmila Baskaran anytime happy to hear it was helpful!

  • @shwetapalande7640
    @shwetapalande7640 5 лет назад +1

    Movie theatre analogy helped. Thanks for sharing

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад

      Anytime Shweta, happy to hear it was helpful :)

  • @jlecampana
    @jlecampana 5 лет назад +2

    Hello Mr. Naughton, I am really looking forward to seeing the Tutorial on Dynamic Programming that you once mentioned it's coming. Thank you for your efforts making these videos.
    Kind Regards!

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад +1

      Hopefully I'll have something and anytime I'm happy to help thank YOU for your support!

  • @MrDivad006
    @MrDivad006 5 лет назад +1

    You can improve memory to >90% by getting rid of the two int variables and returning "return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); " Time is O(n), space also O(n) because we will have one call on the call stack for each node in case the tree is so unbalanced it becomes a list.

  • @PriyanshGupta2024
    @PriyanshGupta2024 Год назад

    The theater analogy! Damn Kevin.

  • @JamesHalpert8555
    @JamesHalpert8555 4 года назад +1

    great explanation!! Kindly make more videos explaining the concept of recursion in depth...because it seems a very troublesome topic.Thank You.

  • @bogdanmazur3256
    @bogdanmazur3256 2 года назад

    Best explanation of recursion EVER! It helped me so much to understand the code you wrote. ThanX, man.

  • @rodolfoganzi2454
    @rodolfoganzi2454 4 года назад +6

    I dont understand how left and right are incremented...

    • @kozie928
      @kozie928 4 года назад +9

      Let's say you have a binary tree with only the left side and a height/max-depth of 3.
      1
      /
      2
      /
      3
      1: recursive call on 2
      2: recursive call on 3
      3: recursive call on null
      3 returns 1+max(0,0) = 1
      2 returns 1+ max(1,0) = 2
      1 finally returns 1+ max(2,0) = 3 (height/max-depth of the tree)
      the return statement inside a recursively called function basically "passes the value up" to the function which called it

    • @ParitoshPotukuchi
      @ParitoshPotukuchi 4 года назад

      @@kozie928 Nice!

  • @BikashAdhikari
    @BikashAdhikari 2 года назад

    next time I am at the movies, I will ask the guy in-front of me, what row they are in

  • @justinf7438
    @justinf7438 5 лет назад +1

    Awesome video! Your analogy made it so much more understandable.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад

      Thanks Justin and I'm so happy to hear that!!!

  • @hellosagar
    @hellosagar 4 года назад

    you disturb the entire theatre
    but nice anology

  • @stevefox7418
    @stevefox7418 4 года назад +6

    How is the count being calculated ? Since I don't see any incrementing going on.

    • @Chiby5570
      @Chiby5570 4 года назад +2

      At the end of each recursive call it is one being added.
      Hope it helps

    • @victoronofiok4551
      @victoronofiok4551 3 года назад +2

      you can visualize this, (you might have figured it out by now but for anyone else wondering)
      the left of node 3 is node 9 -> node 9 is a leaf which means the node 9.left == null ,node9.right == null
      both functions returns 0 because of the base case, so int left = 0 and int right = 0 (for maxDepth(9))
      it then executes the next line which is to return the max of both of its left and right node and since they both returned 0
      therefore Math.max(0, 0) + 1, which is 0 + 1 = 1
      therefore the statement "return Math.max(left, right) + 1", returns 1 for the function maxDepth(9)
      if you follow the same approach on getting to the right of "node 3" which is "20", then maxDepth(20) returns 2
      Then, the root which is the final function in the call stack, has executed both its left and right
      and they have gotten back to 20 with values of int left =1 and int right = 2
      so, the statement "return Math.max(left, right) + 1" for the last function call, maxDepth(3), returns Math.max(1, 2) + 1 => 2 + 1 => 3
      returns 3
      (additional: the right row (node 20) told the node on the root that it is on level 2, which means the root is plus 1 of it from the leaves, therefore level 3)
      Hope this helps!

  • @prashantsingh1096
    @prashantsingh1096 3 года назад

    Thanks for the movie theatre analogy . That make sense :)

  • @sju9227
    @sju9227 5 лет назад +1

    Brilliant analogy

  • @andreiatrigueiro
    @andreiatrigueiro 3 года назад

    I loved the idea of movie theater was brilliant ! Very simple and intuitive. Thank you :)

  • @soumyabiswas6251
    @soumyabiswas6251 4 года назад +1

    Appreciate all the hard word you're putting in - really helpful! :-)

  • @ivanchl
    @ivanchl 4 месяца назад

    Hi. Thank you for explanation. How does a recursive method generate and int value? And how does an int value keeps adding up to give you a total of levels?

  • @solarkenny
    @solarkenny 3 года назад

    let's go to the movie theater to recurse.

  • @An-Engineered-Journey
    @An-Engineered-Journey 3 года назад

    I appreciate your analogies over Nick White's corny jokes (I still love Nick White's videos)

  • @ashrafpatel2238
    @ashrafpatel2238 5 лет назад +2

    Wow, theatre analogy makes sense

  • @nehabhargava
    @nehabhargava 5 лет назад +1

    As always GREAT info, to the point...keep it up!!

  • @swetha8915
    @swetha8915 4 года назад

    Great Analogy with a clear explanation. Yes, Nothing wrong in going to Movies or to Restaurants alone :)
    Thank you for your videos. I subscribed to your channel :)

  • @akankshapatel9743
    @akankshapatel9743 4 года назад

    Amazing analogy and Explanation .. Gonna recommend this channel to my colleagues and friends for sure ! Thanks :)

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 года назад

      akanksha patel thanks Akanksha! If you like my explanations be sure to sign up for the interviewing service I created thedailybyte.dev/?ref=kevin I recommend joining the annual tier if you can!

  • @ivanchl
    @ivanchl 4 месяца назад

    The best explanation ever!!

  • @lucassmith6320
    @lucassmith6320 2 года назад +1

    Thanks for the video, I totally understand the logic, but from the code perspective, would maxDepth(root.left) be called and do the recursion every time when mapDepth(root.right) is called? So for the maxDepth(root.left) is like double recursion?

  • @ahmedaj2000
    @ahmedaj2000 3 года назад +1

    love the analogy!

  • @alanchavez16
    @alanchavez16 5 лет назад +1

    Great explanation!

  • @MustafaDane
    @MustafaDane 5 лет назад +1

    GREAT Analogy!!! Thanks so much!

  • @BamdadGoshtasbi
    @BamdadGoshtasbi 10 месяцев назад

    Thanks, explanation was pretty neat.

  • @Edmundlu
    @Edmundlu 5 лет назад +1

    fire analogy

  • @cesarmelendez2577
    @cesarmelendez2577 2 года назад

    best on youtube. actually

  • @rajatsrivastava555
    @rajatsrivastava555 3 года назад

    Awesome explanation Kevin!!

  • @mabeleain3320
    @mabeleain3320 2 года назад

    you are very good at explain things!

  • @michaeldang8189
    @michaeldang8189 4 года назад

    If the input is a compact array representation of a tree, then the max depth of the tree is simply:
    return log2(array.length) + 1;
    Isn't it? If confirmed the array is not compact, then walk backward to find the first index of non-null value, and then:
    return log2(i) + 1;

  • @dashersuper
    @dashersuper 2 года назад

    Great explanation

  • @raghvendratiwari7862
    @raghvendratiwari7862 5 лет назад

    Very nice example and explanation.you connected problem with real life

  • @the_indian_reads
    @the_indian_reads 4 года назад

    "in my case totally alone" hit me hard

  • @MrSchmidt97
    @MrSchmidt97 2 года назад

    Thank you, following this I finally got it :D

  • @aliadel1723
    @aliadel1723 9 месяцев назад

    Awesome Explanation ♥

  • @ilanaizelman3993
    @ilanaizelman3993 5 лет назад +1

    Gold. Thank again.

  • @seal0118
    @seal0118 4 года назад +1

    is there a possible solution where we like keep a "count" variable and start counting from the root, instead of working our way from down up ?

  • @khhossain3376
    @khhossain3376 2 года назад

    So Math.max(left, right) + 1 compares the left and right subtree heights and returns which one is larger?

  • @robinfelix3879
    @robinfelix3879 2 года назад

    Guys, This might be simple question. But I am trying to visualize multilevel recursion with return statement. Please shed some light for my better understanding
    def maxDepth(self, root: Optional[TreeNode]) -> int:
    if not root:
    return 0
    left_depth = self.maxDepth(root.left)
    right_depth = self.maxDepth(root.right)
    return max(left_depth,right_depth) + 1
    I understood the basics of recursion but the return statement which is adding +1 is haunting me. i couldnt visualize

  • @ianmichael3912
    @ianmichael3912 5 лет назад +1

    Intro music is 🔥

  • @sumitanglekar6309
    @sumitanglekar6309 4 года назад +1

    Genius !

  • @Pre-Omniscient
    @Pre-Omniscient 3 года назад

    I don't understand, Math.max(a,b) returns the maximum of two integers, I understand that once it has gotten to the furthest node in the tree it can propagate back to the root a number of times that will give you the number of nodes it's passed through, but I don't understand how Math.max(a,b) helps with that.

  • @pierre-alexandremousset1913
    @pierre-alexandremousset1913 4 года назад +1

    I don't get smething, where are we adding +1 for each levels?

  • @rohan-rj1ft
    @rohan-rj1ft 3 года назад

    would appreciate if you could also explain the flow of the code with the example. Thanks

  • @amardeepsingh7528
    @amardeepsingh7528 Год назад

    excellent example

  • @יוסייצחק-נ2מ
    @יוסייצחק-נ2מ 2 года назад

    Hi, is there any difference if the tree is balance? i know that the complexity is O(log n) in this case, but i don't understant how can it be if the code is identical? thanks!

  • @aparnagopal5201
    @aparnagopal5201 4 года назад +1

    Seriously 😂 Google is asking u to find the height of the tree😂😂😂😂😂😂

  • @yonghyunjin11
    @yonghyunjin11 3 года назад

    Hi, should I memorize the tree node functions like maxDepth() for the coding interview?

  • @Philo-Tech-0
    @Philo-Tech-0 3 года назад

    Thank you.

  • @fnumeherunnsa7491
    @fnumeherunnsa7491 5 лет назад +1

    great Analogy :)

  • @B-Billy
    @B-Billy 3 года назад

    Why the depth is 3 and not 2? There are only 2 edges from leaf to root.

  • @NazaninAhmady-b4l
    @NazaninAhmady-b4l Год назад

    root.left or root.rigth , what do they return? I mean for example what is left in (int left= maxDepth(root.left)??

    • @deeps-n5y
      @deeps-n5y 5 месяцев назад

      It keeps getting called recursively till the leaf node. At the leaf node it will return 0. This 0 gets carried forward and gets added by 1 for every level traversed in left and right subtree(only max of left and right goes forward). Hope it was clear 😅

  • @mahir7530
    @mahir7530 3 года назад

    what would root.left and root.right return?

  • @longpham338
    @longpham338 5 лет назад +1

    Hey Kevin! Great explanation dude!! Just out of curiosity, have you tried solving this problem using BFS?

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад

      I haven't but I imagine it's just a standard BFS where you just increment the level you're on to find the depth

  • @JamesHalpert8555
    @JamesHalpert8555 4 года назад

    Also...test cases having exactly one node or two nodes are failing with this solution.Please Help.

  • @namratavishwaskelkar6853
    @namratavishwaskelkar6853 2 года назад

    Thanks ! :)

  • @xavicfc1504
    @xavicfc1504 4 года назад

    i got it at the end of the analogy

  • @vinayak186f3
    @vinayak186f3 3 года назад

    1:24 I understand 😐

  • @typef_ce
    @typef_ce 2 года назад

    King

  • @Vty358
    @Vty358 5 лет назад

    hey i am rohit from india i have a query is this approch work as well as min depth ?

  • @SHiiZZANE
    @SHiiZZANE 5 лет назад

    Great video bud :D

  • @rakeshkumar-oj3nj
    @rakeshkumar-oj3nj 3 года назад

    Hey, is this solution more optimized than using BFS to get the answer?

  • @anupbandawar8512
    @anupbandawar8512 5 лет назад

    Please, make a video on palindrome count of given string.

  • @soledapper
    @soledapper 5 лет назад

    I’ll go to the movies with you bro lol

  • @ashokbalakrishnan1371
    @ashokbalakrishnan1371 4 года назад

    Could you provide solution for 849 problem?

  • @90krishika
    @90krishika 5 лет назад

    Hello Sir, can you make a view on leetcode 289. Game of Life
    ?

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад

      Hey Nilanjan I'll see what I can do thanks for the suggestion!

  • @alanl4885
    @alanl4885 5 лет назад

    Hey man can you do LRU Cache?

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 лет назад

      I'll see what I can do thanks for the suggestion Eddy!

  • @davidontiveroz8295
    @davidontiveroz8295 2 года назад

    terrible. just copy pasted solution and no explanation on in-built parent,child,root properties of treenodes represented as arrays, really just terrible. may as well look at a solution and watch 10 minutes of some guy not helping you