Diameter of a binary tree | Leetcode

  Рет қаралды 61,037

Techdose

Techdose

Күн бұрын

Пікірлер: 76
@kharlopena7512
@kharlopena7512 3 жыл бұрын
Thank you so much! I've been looking up leetcode explanations and every time I find one from you it's ALWAYS the clearest and easiest to understand! YOU ROCK!
@shashanktiwari4442
@shashanktiwari4442 4 жыл бұрын
Sudden increase in difficulty lol
@techdose4u
@techdose4u 4 жыл бұрын
😂
@krishnakeshav23
@krishnakeshav23 3 жыл бұрын
awesome ..so basically we need to return height of the tree in the same recursion as max(c1, c2, c3).
@techdose4u
@techdose4u 3 жыл бұрын
👍🏼
@dinner4chiahao
@dinner4chiahao 2 жыл бұрын
Thank you! Much better explanation than the other videos out there.
@techdose4u
@techdose4u 2 жыл бұрын
Welcome :)
@mwshubham
@mwshubham 3 жыл бұрын
Shortness of recursion code is amazing
@techdose4u
@techdose4u 3 жыл бұрын
:)
@siddharthmaurya6793
@siddharthmaurya6793 3 жыл бұрын
@@techdose4u int dia(TreeNode* root,int &r) { int left ,right; if(!root){ return 0; } left= dia(root->left,r); right=dia(root->right,r); r=max(r,left+right+1); return max(left,right)+1; } int diameterOfBinaryTree(TreeNode* root) { int r=-1; dia(root,r); return r-1; } better solutin than yours.....
@akankshagoyal2508
@akankshagoyal2508 3 жыл бұрын
best channel ever
@techdose4u
@techdose4u 3 жыл бұрын
❤️
@gopikumar7888
@gopikumar7888 4 жыл бұрын
//Approach 1 int height(Node *root){ if(root==NULL) return -1; return 1+max(height(root->left),height(root->right)); } Node *diameterTree(Node *root){ if(root==NULL) return; int option1=height(root->left)+height(root->right); int option2=diameter(root->left); int option3=diameter(root->right); return max(option1,max(option2,option3)); } //Approach 2 - Efficient Approach //in pair first ==Height, second==diameter pair DiameterHeightTree(Node *root){ if(root==NULL){ pairp; p.first=0; p.second=0; return p; } pairleftAns=DiameterHeightTree(root->left); pairrightAns=DiameterHeightTree(root->right); int leftDia=leftAns.second; int leftHeight=leftAns.first; int rightDia=rightAns.second; int rightHeight=rightAns.first; int height=1+max(leftHeight,rightHeight); int diameter=max((leftHeight+rightHeight),max(leftDia,rightDia)); pairp; p.first=height; p.second=diameter; return p; }
@techdose4u
@techdose4u 4 жыл бұрын
👍
@Rahul-rp5hk
@Rahul-rp5hk 4 жыл бұрын
Find depth of tree using recursion and add depth of left + right subtree and update it to max
@techdose4u
@techdose4u 4 жыл бұрын
Actually the concept is simple. I made it look complex 😅
@pritamsarkar3371
@pritamsarkar3371 3 жыл бұрын
😂😂😂 omg its crazy
@najimali32
@najimali32 4 жыл бұрын
didn't understand your explanation. I have written a simpler approach & code. Approach - apply post-order traversal & count the no of nodes from both side & our output will be the max of left + right node. class Solution { static int max; // global variable public int diameterOfBinaryTree(TreeNode root) { max =0; int a = diaUtil(root); return max; } static int diaUtil(TreeNode node){ if (node==null){ return 0; } int l = diaUtil(node.left); int r = diaUtil(node.right); max = Math.max(max,l+r); return 1+Math.max(l,r); } }
@techdose4u
@techdose4u 4 жыл бұрын
Thanks for sharing :)
@ryan-bo2xi
@ryan-bo2xi 4 жыл бұрын
What is the variable a ? int a = diaUtil(root); Its confusing . What if my tree has the longest path only on left subtree or right subtree .
@yaswanthalapati8264
@yaswanthalapati8264 3 жыл бұрын
@@ryan-bo2xi that's y we used Math.max(max,l+r) If theres a max distance in left or right subtree then it will already be stored there before u reach the root node ..
@adhyayan2591
@adhyayan2591 3 жыл бұрын
The video put forward the algorithm in a complex way and I understood it. I just have a doubt. Can you please tell me, how you came to the conclusion on which method to use to solve this problem? I want to know how you approached this solution. (More elaborate - how did you think of this algorithm and what is the mathematical or logical approach to it). Thank you.
@suyashmisra7406
@suyashmisra7406 3 жыл бұрын
there is no single answer to your query. in fact, there is at least one more way to attack the problem(which is a very elegant approach but difficult to think of). what generally helps is to consider small test cases ,which often show you how you can approach the problem.using small examples, you can easily guess that in some trees,the diameter will pass through the root,so it's simply a matter of finding the max depth to the left and right of the root and andding them . however, for trees that don't have the root on the diameter, you have to be a little cleverer than that. a very basic way to circumvent this problem can be this : for every node, to store the length of "the diameter that passes through that node" . of course , there is lots of room for optimising this. after some optimisations and thinking, you may get to the optimal solution. i hope you'll find this useful.
@adhyayan2591
@adhyayan2591 3 жыл бұрын
@@suyashmisra7406 Thank you I found it useful.
@crimsoncad3230
@crimsoncad3230 4 жыл бұрын
It was the toughest problem till now in the April Challenge.
@ryan-bo2xi
@ryan-bo2xi 4 жыл бұрын
its not tough when you understand. First find height of both left and right side and add . Next find diameter of left side and diameter of right side. Max(left+right,diameter left,diameter right) www.geeksforgeeks.org/diameter-of-a-binary-tree/
@sleepypanda7172
@sleepypanda7172 3 жыл бұрын
Thanks a lot. Literally mean it, pal.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@anojk1
@anojk1 4 жыл бұрын
Excellent explanation with the good example
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@DreaMagnifier
@DreaMagnifier 4 жыл бұрын
If we take a global max variable,update it by summing the lhs and rhs path , at each point ,will we get answer.
@vishalimanivannan7109
@vishalimanivannan7109 4 жыл бұрын
Hi Tech dose, you have done a pretty decent job :) thanks for your efforts. I think case 2 covers all the cases and case 1 isn't necessary. Please correct me if I'm wrong.
@techdose4u
@techdose4u 4 жыл бұрын
Maybe you are correct. Verify it by just submitting your code. I have unnecessarily made the explanation longer. You can watch my other tree videos as well.
@vishalimanivannan7109
@vishalimanivannan7109 4 жыл бұрын
@@techdose4u not really. Coming up with solution is one thing, explaining the same to others is a whole different deal. Your other videos have been helpful. Thanks.
@vishalimanivannan7109
@vishalimanivannan7109 4 жыл бұрын
I did verify my code and case 1 seems redundant. Thank you
@MGtvMusic
@MGtvMusic 3 жыл бұрын
@@vishalimanivannan7109 Yes same
@shubhankarbhadra131
@shubhankarbhadra131 4 жыл бұрын
superb explanation man! Your way of teaching is cool and easy to grasp also! Thanks for your efforts.
@techdose4u
@techdose4u 4 жыл бұрын
Some did not understand. I am happy that some did 😅
@kunalsoni7681
@kunalsoni7681 4 жыл бұрын
I understand this problem 😇.. and explanations is brilliant thank you sir
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@maruthkamath9304
@maruthkamath9304 2 жыл бұрын
I think this video needs an edit at 6:15, its not max(0,0)+1 for the third case , instead it is 0+0+1.Can we have that corrected please?
@prasadarepalli7449
@prasadarepalli7449 4 жыл бұрын
In example case 2. You have mentioned diameter of tree 10. How that can be? No of nodes are 10 so Diameter would be 9 right ? (nodes = edges + 1)
@techdose4u
@techdose4u 4 жыл бұрын
Correct. It should be 9 in terms of edges and 10 in terms of nodes. Question is to find in terms of edges. So 9 is correct.
@Apoorvpandey
@Apoorvpandey 2 жыл бұрын
What does the pair means? (0,0) for leaf node what is 0,0
@roshanbhattad4493
@roshanbhattad4493 3 жыл бұрын
thank you brother
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@paragroy5359
@paragroy5359 4 жыл бұрын
Nice explanation sir...... is it possible that the iterative solution of this problem is asked in interview or this recursive approach is ok..
@techdose4u
@techdose4u 4 жыл бұрын
Anything is okay as long as you explain a solution.
@apoorvgarg0204
@apoorvgarg0204 4 жыл бұрын
Beautiful Explanation !
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@debaratighatak2211
@debaratighatak2211 3 жыл бұрын
your videos are always really helpful,thank you for the brilliant explanation! 😇
@soumensinha305
@soumensinha305 4 жыл бұрын
Its tough question as compared to previous question
@techdose4u
@techdose4u 4 жыл бұрын
The concept is simple. I made it look complex in the video 😅 but the cases are correct.
@sase1017
@sase1017 4 жыл бұрын
Your explanation covers too much of repeated rambling words for same logic example, instead of focus on give clear overall picture, It just causes confusion, I m sure many people feel the same way that this questions can be shorten in 5 mins give people clear understanding
@techdose4u
@techdose4u 4 жыл бұрын
This question is much easier than how I explained. This is one video which I did not explain well. Try other videos and I hope you will not be disappointed.
@sase1017
@sase1017 4 жыл бұрын
@@techdose4u Thank you for your hard work!
@NoorAli-uh4uq
@NoorAli-uh4uq 4 жыл бұрын
@TECH DOSE you are doing great job.
@piyushlohiya1977
@piyushlohiya1977 3 жыл бұрын
Isn't first solution wrong because just calculating max height will never give the correct answer right?
@vishaljat3314
@vishaljat3314 4 жыл бұрын
just tell me weather it is o(n) approach or not int go(Node* n, int* dia) { if (!n) return 0; int l = go(n->left, dia); // height of left subtree int r = go(n->right, dia); // height of right subtree if (l + r + 1 > *dia) *dia = l + r + 1; // l+r+1 is a possible max dia return 1 + max(l, r); // returning height of subtree } int diameter(Node* node) { int dia = 0; go(node, &dia); return dia; }
@danielbartolini115
@danielbartolini115 Жыл бұрын
can someone please explain what is the space complexity adn why
@cloud15487
@cloud15487 4 жыл бұрын
That question is marked with easy @all.
@techdose4u
@techdose4u 4 жыл бұрын
Should have been medium though
@cloud15487
@cloud15487 4 жыл бұрын
@@techdose4u yup
@pahehepaa4182
@pahehepaa4182 4 жыл бұрын
Wish you could explain a little better for the beginners.
@techdose4u
@techdose4u 4 жыл бұрын
I went too far and explained in too detailed manner than I should have. I realized it. I am trying to improve by every video. Hope you will understand other lectures :)
@perisetlasatwik8139
@perisetlasatwik8139 3 жыл бұрын
abe thumbnail pe leetcode ke logo change karlo PHUB ka logo hai vo 😂😂😂
@ramanshusingh7516
@ramanshusingh7516 3 жыл бұрын
Your code doesnt work on gfg bro
@palakgupta4104
@palakgupta4104 4 жыл бұрын
what are the time and space complexity for this??
@kaushaljalan2928
@kaushaljalan2928 4 жыл бұрын
Explanation wasn't good
@techdose4u
@techdose4u 4 жыл бұрын
It was complex. Should have explained in an easier way.
@liingpangryantee7203
@liingpangryantee7203 3 жыл бұрын
1:50 I believe the diameter of Case 2 should be 9?
@aakashyadav6228
@aakashyadav6228 3 жыл бұрын
Yes
@deveshkumar8823
@deveshkumar8823 3 жыл бұрын
Don't waste your time watching this ...it's too complex
@techdose4u
@techdose4u 3 жыл бұрын
I think this could have been made much easier 😅
@acro0008
@acro0008 4 жыл бұрын
explaination is not so good
@techdose4u
@techdose4u 4 жыл бұрын
Yes true. This is one video where I messed up 😅
Lowest Common Ancestor of a binary tree | Leetcode #236
17:29
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН
Чистка воды совком от денег
00:32
FD Vasya
Рет қаралды 2,4 МЛН
كم بصير عمركم عام ٢٠٢٥😍 #shorts #hasanandnour
00:27
hasan and nour shorts
Рет қаралды 11 МЛН
Diameter of Binary Tree - Leetcode 543 - Trees (Python)
11:16
Diameter of a Binary Tree (Code/ Algorithm)
17:15
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 94 М.
Winning Google Kickstart Round C 2020
30:57
William Lin (tmwilliamlin168)
Рет қаралды 4 МЛН
Binary tree maximum path sum | Leetcode #124
15:23
Techdose
Рет қаралды 55 М.
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 237 М.
L16. Diameter of Binary Tree | C++ | Java
13:47
take U forward
Рет қаралды 379 М.
Vertical order traversal of a binary tree | Leetcode #987
18:03
Maximum Width of Binary Tree - Leetcode 662 - Python
11:38
NeetCodeIO
Рет қаралды 21 М.
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН