Diameter of a binary tree | Leetcode

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

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!
@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 жыл бұрын
👍🏼
@shashanktiwari4442
@shashanktiwari4442 4 жыл бұрын
Sudden increase in difficulty lol
@techdose4u
@techdose4u 4 жыл бұрын
😂
@dinner4chiahao
@dinner4chiahao 2 жыл бұрын
Thank you! Much better explanation than the other videos out there.
@techdose4u
@techdose4u 2 жыл бұрын
Welcome :)
@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 жыл бұрын
👍
@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.
@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.....
@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
@akankshagoyal2508
@akankshagoyal2508 3 жыл бұрын
best channel ever
@techdose4u
@techdose4u 3 жыл бұрын
❤️
@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.
@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/
@Apoorvpandey
@Apoorvpandey 2 жыл бұрын
What does the pair means? (0,0) for leaf node what is 0,0
@sleepypanda7172
@sleepypanda7172 3 жыл бұрын
Thanks a lot. Literally mean it, pal.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@kunalsoni7681
@kunalsoni7681 4 жыл бұрын
I understand this problem 😇.. and explanations is brilliant thank you sir
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@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 ..
@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.
@anojk1
@anojk1 4 жыл бұрын
Excellent explanation with the good example
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@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?
@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; }
@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.
@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 😅
@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.
@piyushlohiya1977
@piyushlohiya1977 3 жыл бұрын
Isn't first solution wrong because just calculating max height will never give the correct answer right?
@danielbartolini115
@danielbartolini115 Жыл бұрын
can someone please explain what is the space complexity adn why
@palakgupta4104
@palakgupta4104 4 жыл бұрын
what are the time and space complexity for this??
@roshanbhattad4493
@roshanbhattad4493 3 жыл бұрын
thank you brother
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@debaratighatak2211
@debaratighatak2211 3 жыл бұрын
your videos are always really helpful,thank you for the brilliant explanation! 😇
@cloud15487
@cloud15487 4 жыл бұрын
That question is marked with easy @all.
@techdose4u
@techdose4u 4 жыл бұрын
Should have been medium though
@cloud15487
@cloud15487 4 жыл бұрын
@@techdose4u yup
@apoorvgarg0204
@apoorvgarg0204 4 жыл бұрын
Beautiful Explanation !
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@liingpangryantee7203
@liingpangryantee7203 3 жыл бұрын
1:50 I believe the diameter of Case 2 should be 9?
@aakashyadav6228
@aakashyadav6228 3 жыл бұрын
Yes
@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 :)
@ramanshusingh7516
@ramanshusingh7516 3 жыл бұрын
Your code doesnt work on gfg bro
@perisetlasatwik8139
@perisetlasatwik8139 3 жыл бұрын
abe thumbnail pe leetcode ke logo change karlo PHUB ka logo hai vo 😂😂😂
@kaushaljalan2928
@kaushaljalan2928 4 жыл бұрын
Explanation wasn't good
@techdose4u
@techdose4u 4 жыл бұрын
It was complex. Should have explained in an easier way.
@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.
@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
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 5 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
Players vs Pitch 🤯
00:26
LE FOOT EN VIDÉO
Рет қаралды 133 МЛН
DIAMETER OF A BINARY TREE | LEETCODE 543 | PYTHON DFS SOLUTION
10:02
543. Diameter of Binary Tree | 3 Ways | Tree | ⭐️IMPORTANT⭐️
21:54
[Java] Leetcode 543. Diameter of Binary Tree [Binary Tree #11]
9:40
Eric Programming
Рет қаралды 8 М.
Diameter of a Binary Tree (Code/ Algorithm)
17:15
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 94 М.
Binary tree maximum path sum | Leetcode #124
15:23
Techdose
Рет қаралды 55 М.
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 237 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 438 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 834 М.
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 5 МЛН