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
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..
😇🙏♥️
The channel is truly a gem. Glad I found this. Thanks a lot. 🙏
Means a lot ❤️
Congratulations 25k 🎉❤
Underrated channel.. first time i am commenting on a video for better reach.
Means a lot ❤️🙏
Man your content is simply too valuable
Every day I eagerly wait for your "Story" even if I solve it by my own. Your "Story" hits different.
You are just amazing.. Non one teaches like you.
Your teachings are increasing my confidence. Thank you bhaiya
Thanks a lot Bhaiya ❤❤ and Congrats for 25k subs 🥳🥹😍
Thank you so much brother ❤️🙏
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
Thanks for explaining so well!
best place to learn DSA
Please illustrate the crux of "Minimum Depth of Binary Tree"
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
Nice video
congratulations 25+k subscribe 👏 🎉❤
Should be marked as Medium.
And beautiful explanation. Story to code
Congo for 25k dude. But believe me this is nothing, in some time you'll be hitting 100k in a month or two.
Best Approach.
Can we do this iteratively using stack?
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)
Congratulations 25k 🎉
congratulation for 25k to my story wale guru drodacharya ❣❣❣
❣❣❣
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
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
@@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!
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.
hey can you please cover regular expression matching
Can you please make video using Morris traversal of this question?
great bideo sir
Thanks ❤❤❤
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;
}
recursion is very difficult to understand. Will try to watch your playlist to understand
26k 🤯⚠️🥳🥳🥳
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));
}
Kon kon reel dekh kae aa rha h
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 😅
hum first hum first 😂
mai
Konsi?
@@bhuppidhamiisomeone gave a shout out to the channel.
But Im following him from 5 months now❤❤❤❤
❤❤❤❤❤
Hello sir, Sir please update the DSA sheet 😣
This approach is not working in gfg practice. the ans is coming out to be one less than the correct answer.
Yes it totally depends how the Qn has defined the diameter. (By counting edge or by counting nodes). Not a problem.
Thank You❤
thank you ❤❤❤
THANKS ❤❤