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:
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 recursive way and iterative way. Both ways have different approaches and different time and space complexities. 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. Code: Output: The output of the above code is: The diameter of the binary tree is 6 In the above code, we have used a recursive function named getDiameter() Function that will be called repeatedly to find the heights of both the left subtree and right subtree of the binary tree then those heights will be used to calculate the diameter of the Binary tree. Because of the repeated recursive calls of the recursive function, the time complexity and space complexity is too high as compared to the iterative way. The time complexity of finding the diameter of binary tree is: O(n^2) The space complexity of finding the diameter of binary tree is: O(log n) 2. Iterative WayInstead of using a recursive function, the binary tree is traversed in a depthfirst search manner to find the diameter of the binary tree. As we are iterating through the binary tree in a depthfirst 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. Code: Output: The output of the abovewritten code is: 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 O(n). The space complexity of the iterative way of finding the diameter of the binary tree is O(n).
Next TopicHeight of Binary Tree
