LeetCode 104. Maximum Depth of Binary Tree - Interview Prep Ep 65

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ก.พ. 2025
  • ⭐ Shop on Amazon to support me: www.amazon.com...
    ⭐ NordVPN to protect your online privacy: go.nordvpn.net...
    ⭐ NordPass to help manage all of your passwords: go.nordpass.io...
    LeetCode 104. Maximum Depth of Binary Tree: leetcode.com/p...
    ⭐ Support my channel and connect with me:
    / @fishercoder
    Solutions explained:
    Recursive solution: We'll add one to each level before traversing to its left and right children, return zero when the current node is null.
    Iterative solution: We can use two extra data structures to help us mimic what happens when the recursive program runs, one is the stack that holds the tree nodes that we'll iterate on, the other is the depths of each of the node.
    // TOOLS THAT I USE:
    ○ Memory Foam Set Keyboard Wrist Rest Pad - amzn.to/3cOGOAj
    ○ Electric Height Adjustable Standing Desk - amzn.to/2S9YexJ
    ○ Apple Magic Keyboard (Wireless, Rechargable) - amzn.to/36gy5FJ
    ○ Apple Magic Trackpad 2 (Wireless, Rechargable) - amzn.to/36ltimu
    ○ Apple MacBook Pro - amzn.to/30iSvKE
    ○ All-In One Printer - amzn.to/34etmSi
    ○ Apple AirPods Pro - amzn.to/2GpVYQf
    ○ My new favorite Apple Watch - amzn.to/2EIIUFd
    // MY FAVORITE BOOKS:
    ○ Introduction to Algorithms - amzn.to/36hxHXD
    ○ Designing Data-Intensive Applications - amzn.to/2S7snOg
    ○ Head First Java - amzn.to/2ScLDKa
    ○ Design Patterns - amzn.to/2SaGeU2
    Follow me on Github for complete LeetCode solutions: github.com/fis...
    Support me on Patreon: / fishercoder
    My ENTIRE Programming Equipment and Computer Science Bookshelf:
    www.amazon.com...
    And make sure you subscribe to my channel!
    Your comments/thoughts/questions/advice will be greatly appreciated!
    #softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures

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

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

    Actually this is the best explanation you can find on youtube....Others just focused on writing the code.Thanks man!

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

    Thanks for explaining this so well. I feel like a lot of coding youtubers don't break down a problem into it's visual side which is so important for learning how something works!

  • @deeps-n5y
    @deeps-n5y 10 หลายเดือนก่อน +1

    Best visualization out there. Thanks

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

    Thank you, I was looking for exactly this type of diagrammatic explanation! Thank you so much!!!

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

    This was the best explanation I could find of this problem, I appreciate you drawing out the recursive calls.

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

      You're very welcome!

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

    best video so far

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

    Excellent explanation. Thank you! Keep up the good work!

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

      Glad it was helpful!

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

    That was the best explanation I've ever seen. Thank you very much!

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

      Glad it was helpful!

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

    Level up! My algorithmic intelligence has been increased by 3.

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

    you are amazing man , love your videos

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

      I appreciate that!

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

    hey, thanks for the great explanation and effort Fisher. I hope you could possibly show a visual explanation for the iterative step as well for those who are not familiar with the LinkedList data structure. Thank you!

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

      Great suggestion!

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

      Yeah, I came here for the visual explanation for the iterative approach :(

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 5 ปีที่แล้ว +5

    Thanks for such a detailed explanation,quite informative video.Please also explain code using C++.Please

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

    I finally understood recursion , thanks buddy

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

    Excellent explanation. Much appreciated!

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

      Glad it was helpful!

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

    that's a great explanation. Thanks !!

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

      Glad it was helpful!

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

    Always the best! Thanks man!

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

    nice!

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

    Hey, I'm having trouble
    I wrote the following code but not sure why it returns the wrong answer
    public int maxDepth(TreeNode root)
    {
    if(root==null) return 0;

    if(root.left!=null) lh = maxDepth(root.left);
    if(root.right!=null) rh = maxDepth(root.right);

    return 1+Math.max(lh,rh);

    }

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

      public int maxDepth(TreeNode root)
      {
      if(root==null) return 0;

      int lh = maxDepth(root.left);
      int rh = maxDepth(root.right);

      return 1+Math.max(lh,rh);

      }

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

    pretty good. BTW, I guess you can speak mandarin, right?

  • @hydrocy.9165
    @hydrocy.9165 7 หลายเดือนก่อน

    int maxDepth(TreeNode* root) {
    int maxDepth = 0; // Initialize the maximum depth
    int count = 0; // Initialize the current depth counter
    dfs(root, count, maxDepth);
    return maxDepth;
    }
    private:
    void dfs(TreeNode* node, int count, int &maxDepth) {
    if (node == NULL) return;
    count++; // Increment counter to reflect current depth
    if (count > maxDepth) {
    maxDepth = count; // Update maximum depth
    }
    dfs(node->left, count, maxDepth);
    dfs(node->right, count, maxDepth);
    }
    }; brother this code works but i cant understand how every recursion call maintain its own count variable