Diameter of Binary Tree | Simple Story | Amazon | Leetcode 543

Поділитися
Вставка
  • Опубліковано 9 лют 2025
  • iPad PDF Notes - github.com/MAZ...
    Whatsapp Community Link : www.whatsapp.c...
    This is the 40th Video of our Playlist "Binary Tree : Popular Interview Problems".
    In this video we will try to solve a very good Binary Tree problem : Diameter of Binary Tree | Leetcode 543
    I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Also, please note that my Github solution link below contains both C++ as well as JAVA code.
    Problem Name : Diameter of Binary Tree
    Company Tags : Amazon, Cadence India, Directi, MakeMyTrip, Microsoft, Oracle, OYO Rooms, Philips, Salesforce, Snapdeal, VMWare
    My solutions on Github(C++ & JAVA) : github.com/MAZ...
    Leetcode Link : leetcode.com/p...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My GitHub Repo for interview preparation : github.com/MAZ...
    Subscribe to my channel : / @codestorywithmik
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    Approach Summary :
    The logic of the diameterOfBinaryTree method is based on a recursive approach to find the diameter of a binary tree. It utilizes the diameter private method to traverse the tree, calculating the length of the longest path passing through each node. Within the diameter method:
    If the current node is null, it returns 0.
    It recursively calculates the diameter of the left and right subtrees.
    It updates the result variable with the maximum diameter encountered so far, considering the path passing through the current node.
    It returns the maximum length between the left and right subtrees plus 1, representing the length of the path from the current node to the deepest leaf.
    Finally, the diameterOfBinaryTree method initializes a variable result with the smallest possible integer value, calls the diameter method to compute the diameter, and returns the maximum diameter found.
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    ✨ Timelines✨
    00:00 - Introduction
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #2024 #newyea

КОМЕНТАРІ • 60

  • @mr.random2001
    @mr.random2001 11 місяців тому +10

    Bro after seeing Instagram shoutout I came here, I am thinking why I haven't got this channel this many days, very good explanation
    Just subscribed after seeing one video only..

  • @ramankr0022
    @ramankr0022 10 місяців тому +6

    The channel is truly a gem. Glad I found this. Thanks a lot. 🙏

  • @anuppatankar4294
    @anuppatankar4294 11 місяців тому +5

    Congratulations 25k 🎉❤

  • @vatsalsrivastava4001
    @vatsalsrivastava4001 2 місяці тому +1

    Underrated channel.. first time i am commenting on a video for better reach.

  • @abhinaysahuu
    @abhinaysahuu 7 місяців тому +2

    Man your content is simply too valuable

  • @joydeep-halder
    @joydeep-halder 11 місяців тому +3

    Every day I eagerly wait for your "Story" even if I solve it by my own. Your "Story" hits different.

  • @gui-codes
    @gui-codes Місяць тому

    You are just amazing.. Non one teaches like you.

  • @ashwin_anand_dev
    @ashwin_anand_dev Місяць тому

    Your teachings are increasing my confidence. Thank you bhaiya

  • @gauravbanerjee2898
    @gauravbanerjee2898 11 місяців тому +5

    Thanks a lot Bhaiya ❤❤ and Congrats for 25k subs 🥳🥹😍

    • @codestorywithMIK
      @codestorywithMIK  11 місяців тому +2

      Thank you so much brother ❤️🙏

    • @nish0798
      @nish0798 11 місяців тому +2

      Bro ek group banao jisme bache doubts discuss kare kai baar aapka video dekhe samaj to aata hai but agar kuch doubts aaye toh dusri logo se discus kare jisme aap bhi reply kar sako taaki doubt aaye to same time resolve ho jaye aur kai questions. Ki agr different approach aaye to discuss kar sake

  • @shreyabisen4729
    @shreyabisen4729 4 місяці тому +1

    Thanks for explaining so well!

  • @dishandshah660
    @dishandshah660 8 місяців тому +2

    best place to learn DSA

  • @ArjunSaxena-wl3qs
    @ArjunSaxena-wl3qs 11 місяців тому +1

    Please illustrate the crux of "Minimum Depth of Binary Tree"

    • @codestorywithMIK
      @codestorywithMIK  11 місяців тому +2

      Here you go Arjun ❤️🙏
      Minimum Depth of Binary Tree | Leetcode-111 | BFS | DFS | AMAZON | Explanation ➕ Live Coding
      ua-cam.com/video/uCvur520ejA/v-deo.html

  • @siddhantswarupmallick6710
    @siddhantswarupmallick6710 6 днів тому +1

    Nice video

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 11 місяців тому +1

    congratulations 25+k subscribe 👏 🎉❤

  • @souravjoshi2293
    @souravjoshi2293 11 місяців тому +1

    Should be marked as Medium.
    And beautiful explanation. Story to code

  • @rushabhlegion2560
    @rushabhlegion2560 11 місяців тому +1

    Congo for 25k dude. But believe me this is nothing, in some time you'll be hitting 100k in a month or two.

  • @dhakadmohi_t
    @dhakadmohi_t 6 місяців тому +1

    Best Approach.

  • @xiaoshen194
    @xiaoshen194 11 місяців тому +1

    Can we do this iteratively using stack?

  • @ArjunSaxena-wl3qs
    @ArjunSaxena-wl3qs 11 місяців тому

    Leetcode 104 :) Maximum depth of B.Tree
    can be done by level order or by recursion, I could make up, the recursive one after the story you narrated
    class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
    if not root :
    return 0
    def helper(root) :

    if not root :
    return 0

    left = helper(root.left)
    right = helper(root.right)

    return max(left,right) + 1

    return helper(root)

  • @Akashkumar_12
    @Akashkumar_12 11 місяців тому

    Congratulations 25k 🎉

  • @harshsingh7713
    @harshsingh7713 11 місяців тому +1

    congratulation for 25k to my story wale guru drodacharya ❣❣❣

  • @pankaj-kalra
    @pankaj-kalra 9 місяців тому +1

    Love your videos brother. Par ye wala question playlist mein bohot pehle hona chahiye tha. These playlists would get much better if they're ordered from fundamentals (such as this) to their application in more tricky problems. Just a suggestion of course

    • @codestorywithMIK
      @codestorywithMIK  9 місяців тому +1

      Hi Pankaj,
      I totally agree with you.
      Actually to fix this thing, I usually create two playlists for every topic :
      1) Concepts Playlist - Contains from basic concepts to expert concepts.
      2) Popular Interview Problems playlist.
      I have created concepts and interview problems playlist for
      1) Graph
      2) Recursion
      3) DP
      Planning soon to create concepts playlist for Tree as well.
      Graph Concepts - ua-cam.com/play/PLpIkg8OmuX-LZB9jYzbbZchk277H5CbdY.html&si=lZG2IJTmSW4kRrx-
      Graph Popular Interview Problems - ua-cam.com/play/PLpIkg8OmuX-I_49pdy1XFY6OcATnxUrrO.html&si=CG2JvGWVmvoSqvWA
      Recursion Concepts - ua-cam.com/play/PLpIkg8OmuX-IBcXsfITH5ql0Lqci1MYPM.html&si=614iI4NyHY-FTeJH
      Recursion Problems (In progress) - ua-cam.com/play/PLpIkg8OmuX-IXOgDP_YYiJFqfCFKFBDkO.html&si=88fBhRnr62OYTnDP
      DP Concepts (In progress) - ua-cam.com/play/PLpIkg8OmuX-JhFpkhgrAwZRtukO0SkwAt.html&si=laFVYy6ep2BkOg0s
      DP Popular interview problems - ua-cam.com/play/PLpIkg8OmuX-L_QqcKB5abYynQbonaNcq3.html&si=VHEn9b-wqTnAVyyi

    • @pankaj-kalra
      @pankaj-kalra 9 місяців тому +2

      @@codestorywithMIK oh is just splendid. I was looking for a good resources for DP before starting it. I'm glad now i won't have to jump directly into leetcoding, and i can ditch the robert sedgwick book altogether. thanks for everything!

  • @kashifhussain1872
    @kashifhussain1872 11 місяців тому +2

    Assalamualaikum bhaiya. Please make a dsa sheet which consists of important questions for which you have made a video already. It would be very helpful. I found your dsa sheet but I can't find videos for it.

  • @shashwatnandan5083
    @shashwatnandan5083 11 місяців тому +1

    hey can you please cover regular expression matching

  • @uday_berad
    @uday_berad 11 місяців тому +1

    Can you please make video using Morris traversal of this question?

  • @mma-dost
    @mma-dost 7 місяців тому +1

    great bideo sir

  • @tutuimam3381
    @tutuimam3381 11 місяців тому +1

    Thanks ❤❤❤

  • @sanjaykatta6499
    @sanjaykatta6499 11 місяців тому

    And I guess, this is a simpler way of coding in java, compared to declaring it as an array to pass it as a reference.BTW, congratulations on reaching 25K subs!!
    int result; //declared it as a global variable
    private int solve(TreeNode root){
    if(root == null) return 0;
    int left = solve(root.left);
    int right = solve(root.right);
    result = Math.max(result, left+right); // we want to check and store the intermediate node's diameter which may be the result.
    return 1+Math.max(left,right); // this is actually similar to calculating the height of a tree
    }
    public int diameterOfBinaryTree(TreeNode root) {
    if(root == null) return 0;
    result = Integer.MIN_VALUE;
    solve(root);
    return result;
    }

  • @amboojmittal2993
    @amboojmittal2993 11 місяців тому

    recursion is very difficult to understand. Will try to watch your playlist to understand

  • @bhuppidhamii
    @bhuppidhamii 11 місяців тому +1

    26k 🤯⚠️🥳🥳🥳

  • @sanjaykatta6499
    @sanjaykatta6499 11 місяців тому +1

    I did this, but I think its not optimal
    int height(TreeNode root){
    if(root == null) return 0;
    return 1+Math.max(height(root.left), height(root.right));
    }
    public int diameterOfBinaryTree(TreeNode root) {
    if(root == null) return 0;
    int left = height(root.left);
    int right = height(root.right);
    int leftd = diameterOfBinaryTree(root.left);
    int rightd = diameterOfBinaryTree(root.right);
    return Math.max(left+right, Math.max(leftd,rightd));
    }

  • @abhaybhosle1811
    @abhaybhosle1811 11 місяців тому +16

    Kon kon reel dekh kae aa rha h

    • @wearevacationuncoverers
      @wearevacationuncoverers 11 місяців тому +6

      Are yaar bade mushkil se ye channel mila tha. Kisi ne zor zor se chillakar Scheme bated kisi instagram reel me 😂.
      Waise Mai LinkedIn me Kisi k shoutout post k through yaha aya tha fir is Legend ka fan hogaya 😅

    • @souravjoshi2293
      @souravjoshi2293 11 місяців тому

      hum first hum first 😂

    • @Adnankhan-zz7xh
      @Adnankhan-zz7xh 11 місяців тому

      mai

    • @bhuppidhamii
      @bhuppidhamii 11 місяців тому

      Konsi?

    • @Ramneet04
      @Ramneet04 11 місяців тому +1

      ​@@bhuppidhamiisomeone gave a shout out to the channel.
      But Im following him from 5 months now❤❤❤❤

  • @codeandtalk6
    @codeandtalk6 11 місяців тому

    ❤❤❤❤❤

  • @ZAVEDHRC76
    @ZAVEDHRC76 11 місяців тому

    Hello sir, Sir please update the DSA sheet 😣

  • @mayanksingh7814
    @mayanksingh7814 3 місяці тому

    This approach is not working in gfg practice. the ans is coming out to be one less than the correct answer.

    • @codestorywithMIK
      @codestorywithMIK  3 місяці тому

      Yes it totally depends how the Qn has defined the diameter. (By counting edge or by counting nodes). Not a problem.

  • @bankaaaaaaai
    @bankaaaaaaai 11 місяців тому +2

    Thank You❤

  • @Adnankhan-zz7xh
    @Adnankhan-zz7xh 11 місяців тому

    thank you ❤❤❤

  • @oqant0424
    @oqant0424 11 місяців тому

    THANKS ❤❤