Hi everyone.
Let's do this question.
Write a function which takes in the root of a binary tree and returns the length of the diameter of
the tree.
The diameter of a binary tree is the length of the longest path between any two nodes in the tree.
It is not necessary for this path to pass through the root of the tree.
The length of a path between two nodes is the number of edges between them.
All right.
So let's go ahead and try to understand this question.
We have to find the diameter of a tree.
And over here it's mentioned that the diameter of a binary tree is the length of the longest path between
any two nodes in the tree.
Now for example, we have this binary tree over here.
Let's try to understand the length between these two nodes which are part of the binary tree.
Now it's defined over here that the length of a path between two nodes is the number of edges between
them.
So let's count the number of edges between these two nodes.
So we can see there are six nodes six edges right one, two, three, four, five and six.
So there are six edges between these two nodes.
And you can see that in this binary tree the longest path between two nodes is this path which which
has six edges between it.
That's why the diameter of this binary tree over here is equal to six.
For example, if you take this node and this node so you can see that it just has one and two two edges
between them.
Or let's take this node and this node.
So 12345.
So there are five edges between these two.
Or let's take the nodes this one and this one.
So between these 21234 and five five edges are there.
So between these two nodes there are six edges.
So this is the longest path.
And hence the diameter of this binary tree over here is equal to six.
Now we have to write a function which will take in the root of a binary tree.
And then it has to return the diameter of that particular binary tree.
So this is the question.
Now let's go ahead and write a few test cases.
Now remember in the interview if you have any clarifying questions you have to ensure that you ask them
to your interviewer.
Now over here because it's pretty straightforward.
Let's proceed and take a look at a few test cases.
Now in the interview, remember, you can also come up with the test cases together with you with your
interviewer.
You can ask him or her whether it's okay if you together come up with test cases.
So this will ensure that you have a very good understanding of the question and what the expectation
is, or the expectation about the output is.
All right.
Now let's say this is the binary tree which is given to you.
Now you can see that the longest path is between 4 and 8, right.
And you have four edges between these two nodes.
So the diameter over here has to be four.
Right now if you take any two other nodes for example, you take this one.
And this one there would be one two and three.
So between these two nodes there are three edges.
So again the longest path is between these two nodes.
And that's why the diameter of this binary tree over here is equal to four.
Now for this binary tree we have already seen that the diameter is equal to six.
And in this case the two edges.
We are considering is this one and this one.
And if you are just given a binary tree with only one node, right.
So in this case the diameter is going to be zero because you don't have any edge between two nodes.
And even if the binary tree is just null in that case also we will return zero.
All right.
So again over here also you don't have any two nodes between which you have edges.
So that's why we will return zero here as well.
All right.
So we have understood the question and we have looked at a few test cases in the next video.
Let's try to think about how we can implement this.
And let's try to think about the algorithm we can use for this.