Maximum Depth of Binary Tree

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ส.ค. 2024
  • For business inquiries email partnerships@k2.codes Discord: bit.ly/K2-discord

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

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

    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

    • @codefire1128
      @codefire1128 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 2 ปีที่แล้ว +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 :)

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

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

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

    This is the best description of recursion on the internet.

  • @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!

  • @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

  • @RiteshSingh-uz2qs
    @RiteshSingh-uz2qs 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)

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

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

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

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

  • @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

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

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

  • @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"

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

    Best explanation of recursion I've ever seen!!

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

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

  • @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!

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

    The theater analogy! Damn 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

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

      Exactly!!! Thanks a ton for pointing out

  • @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!!!

  • @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!

  • @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! :-)

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

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

  • @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 :)

  • @MrDivad006
    @MrDivad006 4 ปีที่แล้ว +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.

  • @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!

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

    Movie theatre analogy helped. Thanks for sharing

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

      Anytime Shweta, happy to hear it was helpful :)

  • @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.

  • @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 3 หลายเดือนก่อน

    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?

  • @An-Engineered-Journey
    @An-Engineered-Journey 3 ปีที่แล้ว

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

  • @ivanchl
    @ivanchl 3 หลายเดือนก่อน

    The best explanation ever!!

  • @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 :)

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

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

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

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

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

    you disturb the entire theatre
    but nice anology

  • @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 3 ปีที่แล้ว

      thanks for the python solution

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

    Brilliant analogy

  • @user-xv3yq1cq1u
    @user-xv3yq1cq1u 9 หลายเดือนก่อน

    Thanks, explanation was pretty neat.

  • @aliadel1723
    @aliadel1723 8 หลายเดือนก่อน

    Awesome Explanation ♥

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

    Thank you, following this I finally got it :D

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

    let's go to the movie theater to recurse.

  • @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!

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

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

    • @kozie928
      @kozie928 3 ปีที่แล้ว +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 3 ปีที่แล้ว

      @@kozie928 Nice!

  • @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?

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

    Great explanation

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

    GREAT Analogy!!! Thanks so much!

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

    Wow, theatre analogy makes sense

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

    Awesome explanation Kevin!!

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

    Great explanation!

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

    love the analogy!

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

    you are very good at explain things!

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

    best on youtube. actually

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

    fire analogy

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

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

  • @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 ?

  • @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;

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

    Gold. Thank again.

  • @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

  • @rohan-rj1ft
    @rohan-rj1ft 3 ปีที่แล้ว

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

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

    Thank you.

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

    Genius !

  • @user-om3ty8vc5g
    @user-om3ty8vc5g 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!

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

    "in my case totally alone" hit me hard

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

      Safari Star ahahah

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

      @@KevinNaughtonJr wait what u true alone, but it got me a reply.

  • @B-Billy
    @B-Billy 3 ปีที่แล้ว

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

  • @pierre-alexandremousset1913
    @pierre-alexandremousset1913 4 ปีที่แล้ว +1

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

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

      I have the same question.

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

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

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

    excellent example

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

    Intro music is 🔥

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

    what would root.left and root.right return?

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

    Thanks ! :)

  • @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.

  • @rakeshkumar-oj3nj
    @rakeshkumar-oj3nj 3 ปีที่แล้ว

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

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

    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 4 หลายเดือนก่อน

      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 😅

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

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

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

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

  • @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

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

    great Analogy :)

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

    King

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

    Great video bud :D

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

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

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

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

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

    Could you provide solution for 849 problem?

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

    I’ll go to the movies with you bro lol

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

    i got it at the end of the analogy

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

    1:24 I understand 😐

  • @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