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

Sdílet
Vložit
  • čas přidán 25. 02. 2024
  • iPad PDF Notes - github.com/MAZHARMIK/Intervie...
    Whatsapp Community Link : www.whatsapp.com/channel/0029...
    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/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/diamete...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
    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

Komentáře • 50

  • @dhakadmohi_t
    @dhakadmohi_t Před 12 hodinami +1

    Best Approach.

  • @mr.random2001
    @mr.random2001 Před 5 měsíci +8

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

  • @abhinaysahumusic6062
    @abhinaysahumusic6062 Před měsícem +2

    Man your content is simply too valuable

  • @anuppatankar4294
    @anuppatankar4294 Před 5 měsíci +5

    Congratulations 25k 🎉❤

  • @mma-dost
    @mma-dost Před měsícem +1

    great bideo sir

  • @dishandshah660
    @dishandshah660 Před měsícem +2

    best place to learn DSA

  • @ramankr0022
    @ramankr0022 Před 3 měsíci +3

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

  • @rushabhlegion2560
    @rushabhlegion2560 Před 5 měsíci +1

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

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 5 měsíci +1

    congratulations 25+k subscribe 👏 🎉❤

  • @bankaaaaaaai
    @bankaaaaaaai Před 5 měsíci +2

    Thank You❤

  • @kashifhussain1872
    @kashifhussain1872 Před 5 měsíci +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.

  • @tutuimam3381
    @tutuimam3381 Před 5 měsíci +1

    Thanks ❤❤❤

  • @joydeep-halder
    @joydeep-halder Před 5 měsíci +2

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

  • @Akashkumar_12
    @Akashkumar_12 Před 5 měsíci

    Congratulations 25k 🎉

  • @souravjoshi2293
    @souravjoshi2293 Před 5 měsíci

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

  • @Adnankhan-zz7xh
    @Adnankhan-zz7xh Před 5 měsíci

    thank you ❤❤❤

  • @oqant0424
    @oqant0424 Před 5 měsíci

    THANKS ❤❤

  • @gauravbanerjee2898
    @gauravbanerjee2898 Před 5 měsíci +5

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

    • @codestorywithMIK
      @codestorywithMIK  Před 5 měsíci +2

      Thank you so much brother ❤️🙏

    • @nish0798
      @nish0798 Před 5 měsíci +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

  • @bhuppidhamii
    @bhuppidhamii Před 5 měsíci +1

    26k 🤯⚠️🥳🥳🥳

  • @harshsingh7713
    @harshsingh7713 Před 5 měsíci

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

  • @ArjunSaxena-wl3qs
    @ArjunSaxena-wl3qs Před 5 měsíci +1

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

    • @codestorywithMIK
      @codestorywithMIK  Před 5 měsíci +2

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

  • @ArjunSaxena-wl3qs
    @ArjunSaxena-wl3qs Před 5 měsíci

    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)

  • @codeandtalk6
    @codeandtalk6 Před 5 měsíci

    ❤❤❤❤❤

  • @shashwatnandan5083
    @shashwatnandan5083 Před 5 měsíci +1

    hey can you please cover regular expression matching

  • @uday_berad
    @uday_berad Před 5 měsíci +1

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

  • @pankaj-kalra
    @pankaj-kalra Před 2 měsíci +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  Před 2 měsíci

      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 - czcams.com/play/PLpIkg8OmuX-LZB9jYzbbZchk277H5CbdY.html&si=lZG2IJTmSW4kRrx-
      Graph Popular Interview Problems - czcams.com/play/PLpIkg8OmuX-I_49pdy1XFY6OcATnxUrrO.html&si=CG2JvGWVmvoSqvWA
      Recursion Concepts - czcams.com/play/PLpIkg8OmuX-IBcXsfITH5ql0Lqci1MYPM.html&si=614iI4NyHY-FTeJH
      Recursion Problems (In progress) - czcams.com/play/PLpIkg8OmuX-IXOgDP_YYiJFqfCFKFBDkO.html&si=88fBhRnr62OYTnDP
      DP Concepts (In progress) - czcams.com/play/PLpIkg8OmuX-JhFpkhgrAwZRtukO0SkwAt.html&si=laFVYy6ep2BkOg0s
      DP Popular interview problems - czcams.com/play/PLpIkg8OmuX-L_QqcKB5abYynQbonaNcq3.html&si=VHEn9b-wqTnAVyyi

    • @pankaj-kalra
      @pankaj-kalra Před 2 měsíci +1

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

  • @amboojmittal2993
    @amboojmittal2993 Před 5 měsíci

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

  • @xiaoshen194
    @xiaoshen194 Před 5 měsíci +1

    Can we do this iteratively using stack?

  • @sanjaykatta6499
    @sanjaykatta6499 Před 5 měsíci

    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;
    }

  • @abhaybhosle1811
    @abhaybhosle1811 Před 5 měsíci +16

    Kon kon reel dekh kae aa rha h

    • @wearevacationuncoverers
      @wearevacationuncoverers Před 5 měsíci +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 Před 5 měsíci

      hum first hum first 😂

    • @Adnankhan-zz7xh
      @Adnankhan-zz7xh Před 5 měsíci

      mai

    • @bhuppidhamii
      @bhuppidhamii Před 5 měsíci

      Konsi?

    • @Ramneet04
      @Ramneet04 Před 5 měsíci +1

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

  • @ZAVEDHRC76
    @ZAVEDHRC76 Před 5 měsíci

    Hello sir, Sir please update the DSA sheet 😣

  • @sanjaykatta6499
    @sanjaykatta6499 Před 5 měsíci

    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));
    }