## Diameter of Binary TreeThe diameter of a binary tree can be defined as the number of edges between the longest paths connecting any two nodes in a binary tree. The diameter of the binary tree is also known as the width of the binary tree. The path represents the diameter of the binary tree may or may not pass through the root of the binary tree. The path includes two of the leaf nodes, among which the diameter is getting calculated. There can be two possibilities for the longest path between two nodes representing the diameter of the binary tree: **Via Root Node:**It will pass through the root node of the binary tree also counts the root node.**Not via a Root Node:**In this case, the chosen path will not pass through the root node of the binary tree and will not count the root node in the path.
In the above figure 1, the longest path is between leaf node 4 and leaf node 6 that will pass through the root node 1. The diameter of the binary tree shown in figure 1 is 6, starting from leaf node 4 to leaf node 6 i.e. node 4 - node 2 - node 1 - node 3 - node 5 - node 6 also covering the root node. Whereas in the binary tree shown in figure 2, the longest path for getting the diameter of the binary tree is starting from the leaf node 5 to the leaf node 6, but not including the root node 1. The diameter of this binary tree is 5 following the path node 5 - node 3 - node 2 - node 4 - node 6 excluding the root node 1. ## Ways to Find Diameter of Binary TreeFinding the diameter of a binary tree is a very common problem in data structures, and there are many ways to find the solution to this problem. So, now we have an idea of how to find the diameter of a binary tree, now let us look at the different ways or approaches for finding the solution to this problem statement. There are two different ways of solving this problem ## 1. Recursive WayIn this way, a recursive function is used to find the height of both the subtrees with the help of this recursive function and then, with the help of the heights of both the subtrees, the diameter of the whole binary is calculated. The recursive method's time and space complexity are higher due to repeated recursive calls of the recursive function. Now let's write a Java Code for finding the diameter of the binary tree with the help of recursion.
The diameter of the binary tree is 6 In the above code, we have used a recursive function named The time complexity of finding the diameter of binary tree is: The space complexity of finding the diameter of binary tree is: ## 2. Iterative WayInstead of using a recursive function, the binary tree is traversed in a depth-first search manner to find the diameter of the binary tree. As we are iterating through the binary tree in a depth-first manner, it will help us find the deepest or the farthest leaf node of the binary tree from which the path to another node will be calculated to get the diameter of the tree. Both time and space complexities are less in this method. Here, iterative way space complexity is less than the recursive way of finding the tree's diameter because there are no repeated recursive calls that will increase these complexities. Now let's write a code for the iterative way of finding the diameter of the binary tree.
The diameter of the given tree is 4 In the above code, we have calculated the diameter of the binary tree by using the iterative way. There are repeated recursive calls in the iterative method. In this method, space and time complexity are way less than the recursive method. The time complexity of the iterative way of finding the diameter of the binary tree is The space complexity of the iterative way of finding the diameter of the binary tree is Next TopicHeight of Binary Tree |