I have written a code for finding diameter of Binary Tree. Need suggestions for the following:
Here is one recursive solution in C++ which gives you the height as well as diameter of binary tree.
struct tree
{
int height = -1;
int diameter = 0;
};
struct tree BSTDiameter(struct node *root)
{
struct tree currentTree, leftTree, rightTree;
if (root == NULL)
{
currentTree.height = -1;
currentTree.diameter = 0;
return currentTree;
}
leftTree = BSTDiameter(root->left);
rightTree = BSTDiameter(root->right);
currentTree.height = ((leftTree.height > rightTree.height) ? leftTree.height : rightTree.height) + 1;
if (leftTree.height == -1 || rightTree.height == -1)
currentTree.diameter = 0;
else
currentTree.diameter = (leftTree.height + rightTree.height + 3) > (rightTree.diameter > leftTree.diameter ? rightTree.diameter : leftTree.diameter) ? (leftTree.height + rightTree.height + 3) : (rightTree.diameter > leftTree.diameter ? rightTree.diameter : leftTree.diameter);
return currentTree;
}
The time complexity of this is O(h) where h is height of the tree. Hope that helped you.
public class NodeWrap{
int height = 0;
int maxLength = 0;
public NodeWrap(int h, int m){
height = s;
maxLength = m;
}
}
public NodeWrap getDiameter(BinaryNode root){
if(root == null){
return new NodeWrap(0, 0);
}
NodeWrap left = getDiameter(root.left);
NodeWrap right = getDiameter(root.right);
int height = Math.max(left.height + right.height) + 1;
int maxLength = Math.max(left.maxLength, right.maxLength);
if(left.height != 0 && right.height != 0){
maxLength = Math.max(left.height + right.height + 1, maxLength);
}
return new NodeWrap(singleLength, maxLength);
}
The most efficient way is to compute diameter along with height to get to O(n) and here is the easiest way to get so [Python3,PyPy3]
for this definition for a binary tree node,
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def diameterOfBinaryTree(self, root: TreeNode) -> int:
self.height = 1
def height(node):
if node is None:
return 0
l_height = height(node.left)
r_height = height(node.right)
self.height = max(self.height,l_height+r_height+1)
return max(l_height,r_height) + 1
height(root)
return self.height-1
The most simplest and affordable solution with faster runtime and lower complexity.
You don't need to store the result in the static field diameter. Simply use the static method like that:
public class DiameterOfTree {
public static long getDiameter(BinaryTreeNode root) {
if (root != null) {
long leftDiameter = getDiameter(root.getLeft());
long rightDiameter = getDiameter(root.getRight());
long leftHeight = getHeight(root.getLeft());
long rightHeight = getHeight(root.getRight());
return Math.max(leftHeight + rightHeight + 1, Math.max(leftDiameter, rightDiameter));
}
return 0;
}
public static long getHeight(BinaryTreeNode root) {
if (root != null) {
long leftHeight = getHeight(root.getLeft());
long rightHeight = getHeight(root.getRight());
return 1 + Math.max(leftHeight, rightHeight);
}
return 0;
}
}
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.