Diameter of a Binary Tree | Solution | Binary Tree | Data Structures and Algorithms in JAVA

preview_player
Показать описание

NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we explain the solution of where we are required to find the diameter of a binary tree in minimum time and space complexity. To understand more about this question, click here:

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

Please everyone make this channel as famous as possible.

ashutoshjha
Автор

Love your explanation sir, the way of explanation and your patience is commendable.

abhicodes
Автор

Thank you so much sir for such a clear explanation. I can now provide the interviewer with 3 different approaches for finding diameter (including generic tree approach). All thanks to you ❤

sudhanshusharma
Автор

Sir i did this question using the same approach that we folllowed in Generic Tree
//The approach
static int dia=0;
public static int diameter1(Node node) {
if(node==null){
return -1;
}
int lh=-1;
int slh=-1;

int lch=diameter1(node.left);
if(lch>lh){
slh=lh;
lh=lch;
}
if(lch>slh){
slh=lch;
}
int rch=diameter1(node.right);
if(rch>lh){
slh=lh;
lh=rch;
}
if(rch>slh){
slh=rch;
}

if(lh+slh+2>dia){
dia=lh+slh+2;
}
lh++;
return lh;
// write your code here
}
Then print dia in the main function

architsharma
Автор

Gave this question 2 hrs and totally worth.
Great explanation Sir
Great teacher

harshitkaushik
Автор

Awesome explanation your videos explanation are so down to earth, so simple, raw explanation I feel like my friend is teaching me.🔥🔥❤️❤️

vikashgupta
Автор

Suppose A is the Root node, Then Diameter will either include that node or exclude that node.
Then the
Three factor are:

1)If the diameter include(Passes through) the root, then the diameter will be Lheight+Rheght+2

2)Diameter exclude the root and lies in the left subtree
3)Diameter Exclude the root and lies in the right subtree
Hence Overall Diameter will be maximum of these three factors.

siddharthdwivedi
Автор

Tricky solution 😅. Do baar dekhna pad gya 2nd method ke liye. As always Thanks sir.❤️

AnkitSharma-wjtb
Автор

sir inefficient ki time complexity o(n^2) nhi hai o(nh) where h is the height of the tree
because if we visit every node for every particular node then it would be O(n^2)
but here we are visiting till height of tree from that particular node so TC will be O(nh)

//first = height // second = diameter
pair<int, int>
{
if(root == NULL)
{
pair<int, int> bc(0, 0);
return bc;
}
pair<int, int> lans = HeightDiameter(root->left);
pair<int, int> rans = HeightDiameter(root->right);

pair<int, int> ans;
ans.first = 1 + max(lans.first, rans.first);
ans.second = max(lans.first+rans.first, max(rans.second, lans.second));

return ans;
}

ritikshrivastava
Автор

the length of the video is slightly more than others but it is definitely worth it,
thanks sir.

lokeshnegi
Автор

Magic kya tha ? Sir, magic toh aap ho !🙏

sanatasneem
Автор

Accha kaam ker rahe ho dost...Tareeka bahut acha hai...

johnangelo
Автор

One of the deepest discussion on binary tree

abhishekvishwakarma
Автор

Everything is detailed amazing Content 🔥

vsbelieve
Автор

Mza aa gya sir ye video dekke. Iska approach to mai ab kbhi nhi bhulunga. Tilt wala question ke videos kb upload honge sir?

ashishjha
Автор

basically sir humne height ka function use karne ki jagah DiaPair waale function mein hi height calculate karli dia ke saath saath.

amanpreetsinghsetia
Автор

Sir this can easily be done in O(n) using a static variable:-
static int maxDiam=Integer.MIN_VALUE;
public static int diameter1(Node node) {
// write your code here
if(node==null){
return -1;
}

int ld = diameter1(node.left)+1;
//height of left node till current node
int rd = diameter1(node.right)+1;
// height of right node till current node
int cd = ld+rd;
//current node diameter
if(cd>maxDiam)
maxDiam=cd;
//updating max diameter

return Math.max(ld, rd);
//returning the max of left height and right height, this value will help its parent node
}

rajeev
Автор

That was really helpful, Thank you sir

mohiniranadance
Автор

I love your mathematics I code in c++ but watch your videos for understanding the concept...

Thanks man

garima
Автор

sir jaise humne generic tree me nikala tha diameter vaise nhi kr sakte ?

multiplewaya-qkuq
welcome to shbcf.ru