Diameter of a Binary Tree (Code/ Algorithm)

preview_player
Показать описание
Find the diameter of a binary tree. The number of nodes on the longest path in a binary tree is the diameter. This is a recursive code.

Рекомендации по теме
Комментарии
Автор

This is a great video, perfect speed for someone new and with English second language. Thank you for making it so easy to understand!

irynasherepot
Автор

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

mareimorsy
Автор

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.

praveenchouhan
Автор

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.

opeyemijonah
Автор

Very good explanation! The other guys were confusing until I stumbled upon this guy's video.

pahehepaa
Автор

Sir you have perfect explanation for every can write your own book....you explain better than best books out there of data structures

Adityasharma-oezp
Автор

You are blessed with teaching. I wish I had found this video earlier.

opeyemijonah
Автор

just wow! I was struggling so much and you made this crystal clear. Thankyou! Great video~!

jeezradz
Автор

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 =
const diameterRight =
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);
}

griffinbaker
Автор

Sir Your explanation is nice , every person can easily understand

AbhiKumar-yksr
Автор

Best Explaination so far. Thankyou sir

jhanvibatra
Автор

You Explain like a PRO..Thanks for such a detailed explanation!

ChandraShekhar-bycd
Автор

Most thorough explanation out there! Great job!

jacktrainer
Автор

Thank youu. I was really struggling to understand this and you explained it beautifully

elebs_d
Автор

Vivek, that is a great video. Great explanation that is very much personalized for everyone. Thank you!

pauldaviddavies
Автор

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(lDiameter, rDiameter))

TheSridharraj
Автор

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

aruneshsrivastava
Автор

You are an awesome teacher! Keep up the good work.

kushagrashekhawat
Автор

Khup Sunder Explain Kelat!!! Dhanyavad

shreyasangane
Автор

this is the best explanation i was looking for u. thank u for making it so simple :)

pratapkumarchandra
visit shbcf.ru