This is a great video, perfect speed for someone new and with English second language. Thank you for making it so easy to understand!
@unboxingzindagi99724 жыл бұрын
Totally Agree.He is so real explanation ,no hussle and .He is so real!!
@tsupreetsingh4 жыл бұрын
True
@opeyemijonah94802 жыл бұрын
I appreciate your solution, it was the only one I could understand on this topic. FYI, I translated this to JavaScript. However, for the final result portion, I removed '+1' from the height equation in the diameter function because the height function already includes a +1 to the left or right tree. It passed all cases when I did that.
@User-nq9ee Жыл бұрын
Probably, you solved on leet code, where is not nodes but the most number of edges. which are 1 less than the number of nodes.
@praveenchouhan63884 жыл бұрын
your way of slow explanation is very effective and efficient as these recursive problems are understood best when someone explains slowly other wise its difficult to keep track of recursive call flows, so keep going the way you are and keeping sharing knowledge like you are doing now.
@TheSridharraj3 жыл бұрын
Just one correction or making everyone aware. when calculating the height of the tree, you should include the root too. hence in the diameter math you can simply do return Math.max(leftHeight+rightHeight , Math.max(lDiameter, rDiameter))
@kulaudo3 жыл бұрын
True, with the video from the same problem description for the height would be confusing.
@Adityasharma-oe8zp3 жыл бұрын
Sir you have perfect explanation for every problem.....you can write your own book....you explain better than best books out there of data structures
@mareimorsy31822 жыл бұрын
I watched 10s of videos which is explaining the same problem, your video is the only one that made me finally get it, You're an awesome teacher, keep going
@ABU_BEKIR_SIDDIK6 жыл бұрын
Just play this video 1.5 speed.
@agyatanonymous39956 жыл бұрын
2x works as just perfectly!
@humblecoder91195 жыл бұрын
LOL ! But, he is a great teacher and doing awesome job ! Thank you !
@ishan_kumar5 жыл бұрын
I wish there is 3X option.
@sandeepvedavyas87014 жыл бұрын
@@ishan_kumar press ctrl + shift + j. In the console window type document.getElementsByTagName("video")[0].playbackRate = 3.0
@kartikaygoel30424 жыл бұрын
@@ishan_kumar U can also add video speed controller extension
@pahehepaa41824 жыл бұрын
Very good explanation! The other guys were confusing until I stumbled upon this guy's video.
@staffeng3 жыл бұрын
One thing to note here is the repeated calculation of height is inefficient. So if you're coding it out, you should return a pair "height+diameter" for every subtree. But I understand that Vivekanand (OP, instructor) here omitted it because it distracts us from the main goal of finding the diameter here.
@aruneshsrivastava71542 жыл бұрын
correct solution , other than the fact is maximum diameter could be number of edges in the path , not the number of nodes, in that case we don't have to add extra 1 to the heights
@griffinbaker97922 жыл бұрын
Implementation in JS..it works! Thank you sir. function binaryTreeDiameter(tree) { // Write your code here. if (!tree) return 0; const heightOfLeft = height(tree.left); const heightOfRight = height(tree.right); const heightOfNode = 1 + Math.max(heightOfLeft, heightOfRight); const diameterLeft = binaryTreeDiameter(tree.left); const diameterRight = binaryTreeDiameter(tree.right); const pathThroughRoot = heightOfRight + heightOfLeft; const maxDiameterBelow = Math.max(diameterLeft, diameterRight); return Math.max(maxDiameterBelow, pathThroughRoot); } function height(tree) { if (!tree) return 0; const left = height(tree.left); const right = height(tree.right); return 1 + Math.max(left, right); }
@AbhiKumar-yk9sr2 жыл бұрын
Sir Your explanation is nice , every person can easily understand
@ytuser6594 жыл бұрын
Great explanation, loved it. But I just want to make a point that this is not the most efficient solution. For a given node, we are calculating the height and the diameter separately when you can get the diameter while calculating the height.
@kasyapdharanikota85703 жыл бұрын
that is true , height can be calculated while calculating the diameter by just adding one more parameter .
@jeezradz4 жыл бұрын
just wow! I was struggling so much and you made this crystal clear. Thankyou! Great video~!
@opeyemijonah85302 жыл бұрын
You are blessed with teaching. I wish I had found this video earlier.
@ChandraShekhar-by3cd4 жыл бұрын
You Explain like a PRO..Thanks for such a detailed explanation!
@jacktrainer43874 жыл бұрын
Most thorough explanation out there! Great job!
@pauldaviddavies3 жыл бұрын
Vivek, that is a great video. Great explanation that is very much personalized for everyone. Thank you!
@jhanvibatra8283 жыл бұрын
Best Explaination so far. Thankyou sir
@elebs_d2 жыл бұрын
Thank youu. I was really struggling to understand this and you explained it beautifully
@thelasttimeitookashowerwas70694 жыл бұрын
finding a recursive solution is extremely tough ):
@nivedithat47454 жыл бұрын
I kinda like how creative your username is😂... Made my day😂😂
@jatin16883 жыл бұрын
Hmm it is difficult to find recursive one
@K_EC_AushoRoup4 жыл бұрын
Thanks sir, I was just forgetting the 2nd case when diameter is not passing through the root. Not I got it. Thanks
@saurabhsen35604 жыл бұрын
Can you make a video on "Maximum sum of nodes in Binary tree such that no two are adjacent" I am not able to understand this question and it was asked in the interview of my senior. So please sir a humble request from you from a subscriber please make a video on this question. It will be really helpful for students like us. Thank you for teaching...Your videos are super awsome.
@preetisaroha31185 жыл бұрын
Diameter of a tree can be calculated by only using the height function, because the diameter of a tree is nothing but maximum value of (left_height + right_height + 1) for each node. So we need to calculate this value (left_height + right_height + 1) for each node and update the result. Time complexity - O(n)
@JJ-Bond5 жыл бұрын
could u please show how to do this? i couldn't figure this out myself
@anig34654 жыл бұрын
NARASIMHA KARUMANCHI. THANKS FOR DETAIL EXPLANATION
@prateekkanujiya97755 жыл бұрын
This is brute Force . You need to go for optimal solution
@User-nq9ee Жыл бұрын
Beautifully explained :) Thank you.
@DismalDante6 жыл бұрын
Good video. You can get a O(n) time algorithm by getting the diameter and height in the same recursive call. Just pass by reference the height in the diameter function.
@JJ-Bond5 жыл бұрын
can you please show how to do this? i couldn't figure it out myself...
@shreyasangane38224 жыл бұрын
Khup Sunder Explain Kelat!!! Dhanyavad
@rsKayiira3 жыл бұрын
This code doesn't seem to work unless you write it like this. You adjust the return statements to -1 and +2 class Solution { public int height(TreeNode root) { if(root==null) return -1; else{ int left = height(root.left); int right = height(root.right); return Math.max(left,right)+1; } } public int diameterOfBinaryTree(TreeNode root) { // base case if tree is empty if (root == null) return 0; // get the height of left and right sub-trees int lh = height(root.left); int rh = height(root.right); // get the diameter of left and right sub-trees int ld = diameterOfBinaryTree(root.left); int rd = diameterOfBinaryTree(root.right); /* Return max of following three 1) Diameter of left subtree 2) Diameter of right subtree 3) Height of left subtree + height of right subtree + 1 */ return Math.max(lh + rh + 2, Math.max(ld, rd)); } }
@kmlx194 ай бұрын
The explanation is really clear, but I have a doubt about the time complexity of this solution, it would be O(n²) or O(n)?
@vadirajjahagirdar26157 жыл бұрын
Best explanation out there. Thanks Vivekanand.
@ghostpieces23624 жыл бұрын
Could you follow-up with how do solve Leetcode 1245: Tree Diameter, which is similar to this? Thanks for the great instructional videos.
@user-zj9pq5xc7x4 ай бұрын
amazing. thank you
@abhisekmandal15915 жыл бұрын
good tutorial but this is O(n-square) solution. O(n) solution can be obtained by using height function. Code below: class Tree { /* Complete the function to get diameter of a binary tree */ int diameter(Node root) { Res r = new Res(); diameter(root,r); return r.val; } int diameter(Node root, Res res) { if(root==null) return 0; int lh = diameter(root.left,res); int rh = diameter(root.right,res); res.val = Math.max(res.val, lh+rh+1); return Math.max(lh,rh)+1; } }
@rakeshroshan8295 жыл бұрын
great explanation. Thank you vivekanand fo such a good video.
@kushagrashekhawat82274 жыл бұрын
You are an awesome teacher! Keep up the good work.
@yvanpearson7024 Жыл бұрын
In first example some people would say that diameter is 8 not 9.
@GAURAVKR19865 жыл бұрын
Really nice explanation keep up the good work buddy!
@learntogether-growtogether8657 жыл бұрын
Problem is very well explained ....gr8 Sir
@dhruvilprajapati4734 Жыл бұрын
thanks for lucid explanation
@vcfirefox2 жыл бұрын
SUPREME explanation. thanks!!
@karansachdeva85163 жыл бұрын
Great explanation
@biswaMastAadmi2 жыл бұрын
beautiful explanation
@hiteshdayaramani3887 жыл бұрын
thank you for explaining the concept of diameter of binary tree!
@annieonee3 жыл бұрын
wow best explanation so far. Thank you!
@mohdanaskhan72036 жыл бұрын
your videos are amazing, you just need to put them in order so that it would be easier for the viewer.
@dharanyuvi69513 жыл бұрын
Gist: Max of ( Diameter passing through root, Max diameter not passing through root )
@shashwatagrawal84125 жыл бұрын
Thank you Bhaiya. Doubt cleared. 😊 Please make more videos. Very helpful.
@preetisaroha31185 жыл бұрын
Very nice explanation sir.I really appreciate your efforts.Thanks.
@prashantnagrurkar27844 жыл бұрын
Thanks a lot! 😊 A very good explanation 👌 Perfect speed for me
@mchandresh7 жыл бұрын
you make things so simple bro. thank you for sharing. I am fan of you. could you please sometime post video on eigen value and eigen vectors
@vicky8883 жыл бұрын
Great video!!!
@saivigneshrajendran8677 жыл бұрын
I am a very big fan of you bro.. plz make more videos.. and my humble request to you is that it will very much helpful if you consider some java while explain the code... Thanks from coimbatore.
@pratapkumarchandra64884 жыл бұрын
this is the best explanation i was looking for u. thank u for making it so simple :)
@darod60984 жыл бұрын
I only see this video and subscribed. Just EXCELLENT.
@chetanjain50972 жыл бұрын
Could you please post code as well so that we can copy parts of it and test it out ourselves?
@parthshah63434 жыл бұрын
Superb explanation!
@griffinbaker97922 жыл бұрын
The time complexity of this algo is O(N^2) right? Because we have to calculate the height as well as the diameter for each given node, and for both we need to make recursive calls down the tree.
@pranavbhat928 ай бұрын
Also, where is the height function here? I was looking for the same comment
@abhisheka2p26 жыл бұрын
You're great! Hope u get many likes and subs.
@alfredoluco1893 жыл бұрын
Great video and great explanation. Thanks a lot!
@DeviDevi-yr2sv3 жыл бұрын
Great explaination
@rakeshroshan8295 жыл бұрын
can u please make a video on RED-BLACK Tree
@TheSridharraj3 жыл бұрын
Complexity is O(n^2) right?
@abhishekjain71736 жыл бұрын
Very nice explanation !
@kasyapdharanikota85703 жыл бұрын
very well explained
@Amanyadav-ec3hn4 жыл бұрын
Thanks for the video. Also, try to explain the time complexity of the solution.
I am very big fan of yours. Please keep up the good work.
@yvanpearson7024 Жыл бұрын
this is a O(N^2) solution, i would only study O(N) solutions
@georgebrooking38203 жыл бұрын
YOU ARE A HERO!!!!!!!
@ShivangiSingh-wc3gk6 жыл бұрын
Dear Professor, Please can you make a video on how to find the maximum width of a binary tree. Thank You
@agyatanonymous39956 жыл бұрын
level order traversal might be the answer for this
@alishagarg1504 жыл бұрын
Sir there is a problem with this explanation.In case of an AVL tree where the diameter is supposed to be zero,this gives out some other ans.Kindly check on that!!
@Adityasharma-oe8zp3 жыл бұрын
He is god of algorithms....❤️❤️
@coderajput18167 жыл бұрын
your explanation is good but i think u should use stack also to run each steps thank u 4 this video
@a7medk7alid7 Жыл бұрын
C++ code class Solution { public: int diameterOfBinaryTree(TreeNode* root) { if (!root) return 0; int lheight = height(root->left); int rheight = height(root->right); int ldiameter = diameterOfBinaryTree(root->left); int rdiameter = diameterOfBinaryTree(root->right); return max((lheight + rheight), max(ldiameter, rdiameter)); } int height(TreeNode* root){ if (!root) return 0; int lheight = height(root->left); int rheight = height(root->right); if (lheight > rheight) return(lheight+1); return rheight+1; } };
@reviewsonly4894 Жыл бұрын
thanks bri im a iitian very beginnner in coding thanks
@azad13004 жыл бұрын
While calculating diameter , why are not doing +1 ? if so will the diameter value get increment ?
@remyb99376 жыл бұрын
Excellent explanation
@animemaker74336 жыл бұрын
why did u stop making videos??? You are damn good man
@sivaganesh44894 жыл бұрын
yes
@swagatpatra21394 жыл бұрын
int maxDia; int diameter(TreeNode root) { diaHelper(root); return maxDia; } int diaHelper(TreeNode root){ if(root == null) return 0; int left = diaHelper(root.left); int right = diaHelper(root.right); maxDia = Math.max(maxDia, left + right+1); return Math.max(left,right)+1; }
@johngerassimou88989 ай бұрын
Very good! Thank you sir.
@tonyz22034 жыл бұрын
Good explanation, thank you!
@anamikaahmed48874 жыл бұрын
Well explained! Thank you for this!
@jegatheeshm934 жыл бұрын
Good Explanation.
@one_percent_up4 жыл бұрын
Just to the understading way to the concepts
@veerrajuyeleti85416 жыл бұрын
sir can u do a video on find the largest bst(binary search tree) in a binary tree .
@jeezradz4 жыл бұрын
what is the TIME COMPLEXITY for this?
@TheBreyHD4 жыл бұрын
O(n^2)
@TianmenDream5 жыл бұрын
What's the time complexity of your proposed solution? Looks like quite hard to analyze.
@bonkers44905 жыл бұрын
Time complexity is O(2n) i.e O(n) where n is the number of nodes in the tree. In all the recursive calls, you find the height of left and right subtree by visiting each node at most once. Then you find the diameter of left subtree and right subtree considering root as any node in tree except the root of original tree, and this makes you visit n nodes again. Therefore, 2n which is none other than O(n).
@GauravKawatrakir3 жыл бұрын
This solution takes O(n2) "n square".
@retrogame31384 жыл бұрын
why you stopped to upload new videos
@shrad66115 жыл бұрын
Is I am the only one who give reply of his hello when in good mood :) hahaha lol
@sameedyousuf60364 жыл бұрын
lol i said okay every time he said "okay?"
@goldent46553 жыл бұрын
What's the time and space complexity?
@niharikapatil9024 жыл бұрын
You are soooo gooooddd at this
@dileepmatha80767 жыл бұрын
how to find sum of nodes of a diameter ????
@Atpugtihsrah6 жыл бұрын
Can you explain the O(n) solution?
@piyushchaudhari3974 жыл бұрын
Great work !
@PradeepKumar-so5wq5 жыл бұрын
nice explaination man keep posting .post some greedy method videos