I have written a code for finding diameter of Binary Tree. Need suggestions for the following:
Is the algorithm fine/any suggestions?
public class DiameterOfTree {
public static int diameter = 0;
public static int getDiameter(BinaryTreeNode root) {
if (root != null) {
int leftCount = getDiameter(root.getLeft());
int rightCount = getDiameter(root.getRight());
if (leftCount + rightCount > diameter) {
diameter = leftCount + rightCount;
System.out.println("---diameter------------->" + diameter);
}
if ( leftCount > rightCount) {
return leftCount + 1;
}
return rightCount + 1;
}
return 0;
}
}
Finally, the diameter is maximum among all maximum node paths for every node in the tree. The time complexity of this solution is O(n2) as there are n nodes in the tree, and for every node, we are calculating the height of its left and right subtree that takes O(n) time.
The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root . The length of a path between two nodes is represented by the number of edges between them.
In what time complexity can we find the diameter of a binary tree optimally? Answer - A) We can compute the diameter of a binary tree using a single DFS, which takes the time of O(V + E). 7.
If there are n nodes in a binary search tree, maximum height of the binary search tree is n-1 and minimum height is ceil(log2(n+1))-1.
There are three cases to consider when trying to find the longest path between two nodes in a binary tree (diameter):
The longest path through the root is simply the sum of the heights of the left and right sub-trees (+1 for the root not necessary since the diameter of a tree with a root node and 1 left, 1 right subtree nodes will be 2), and the other two can be found recursively:
public static int getDiameter(BinaryTreeNode root) {
if (root == null)
return 0;
int rootDiameter = getHeight(root.getLeft()) + getHeight(root.getRight()); //Removing the +1
int leftDiameter = getDiameter(root.getLeft());
int rightDiameter = getDiameter(root.getRight());
return Math.max(rootDiameter, Math.max(leftDiameter, rightDiameter));
}
public static int getHeight(BinaryTreeNode root) {
if (root == null)
return 0;
return Math.max(getHeight(root.getLeft()), getHeight(root.getRight())) + 1;
}
Here is an O(n) solution with minimal changes to the accepted answer:
public static int[] getDiameter(BinaryTreeNode root) {
int[] result = new int[]{0,0}; //1st element: diameter, 2nd: height
if (root == null) return result;
int[] leftResult = getDiameter(root.getLeft());
int[] rightResult = getDiameter(root.getRight());
int height = Math.max(leftResult[1], rightResult[1]) + 1;
int rootDiameter = leftResult[1] + rightResult[1] + 1;
int leftDiameter = leftResult[0];
int rightDiameter = rightResult[0];
result[0] = Math.max(rootDiameter, Math.max(leftDiameter, rightDiameter));
result[1] = height;
return result;
}
It just calculates height and diameter at the same time. And since Java does not have pass-by-reference I defined an int[] to return the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With