Untitled
unknown
plain_text
10 months ago
2.5 kB
4
Indexable
// Node class for the binary tree
class Node {
int data;
Node left;
Node right;
// Constructor to initialize
// the node with a value
Node(int val) {
data = val;
left = null;
right = null;
}
}
class Solution {
// Function to find the
// diameter of a binary tree
public int diameterOfBinaryTree(Node root) {
// Initialize the variable to
// store the diameter of the tree
int[] diameter = new int[1];
diameter[0] = 0;
// Call the height function to traverse
// the tree and calculate diameter
height(root, diameter);
// Return the calculated diameter
return diameter[0];
}
// Function to calculate the height of
// the tree and update the diameter
private int height(Node node, int[] diameter) {
// Base case: If the node is null,
// return 0 indicating the
// height of an empty tree
if (node == null) {
return 0;
}
// Recursively calculate the
// height of left and right subtrees
int[] lh = new int[1];
int[] rh = new int[1];
lh[0] = height(node.left, diameter);
rh[0] = height(node.right, diameter);
// Update the diameter with the maximum
// of current diameter or sum of
// left and right heights
diameter[0] = Math.max(diameter[0], lh[0] + rh[0]);
// Return the height of
// the current node's subtree
return 1 + Math.max(lh[0], rh[0]);
}
}
// Main class
public class Main {
// Main function
public static void main(String[] args) {
// Creating a sample binary tree
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.left.right.right = new Node(6);
root.left.right.right.right = new Node(7);
// Creating an instance of the Solution class
Solution solution = new Solution();
// Calculate the diameter of the binary tree
int diameter = solution.diameterOfBinaryTree(root);
System.out.println("The diameter of the binary tree is: " + diameter);
}
}
Editor is loading...
Leave a Comment