Java program to find the nodes which are at the maximum distance in a Binary Tree17 Mar 2025 | 5 min read In this program, we need to find out the nodes which are at the maximum distance in the binary tree. According to our approach, all the distances between all the nodes of the tree will be kept in the variable distance. The distance with the maximum value will be kept by using the variable MaxDistance. Initially, MaxDistance is initialized with the value of distance. If any value is found greater than MaxDistance, overwrite the value of MaxDistance. Repeat this process until we find the maximum possible distance between two nodes of the tree. The algorithm of the process is given below. ![]() Algorithm
a. calculateSize() will count the number of nodes present in the tree. b. convertBTtoArray() will convert the binary tree to its array representation by traversing the tree and adding elements to treeArray. c. getDistance() will calculate the distance of a given node from the root. d. LowestCommonAncestor() will find out the lowest common ancestor for the nodes n1 and n2.
a. FindDistance() will calculate the distance between two nodes.
Program:Output: Nodes which are at maximum distance: ( 4, 9 ) ( 5, 9 ) Next TopicJava Programs |
In this program, we need to check whether all the leaves of the given binary tree are at same level or not. A Node is said to be leaf if it doesn't have any child node. In the below diagram, nodes 4, 5 and 6 are...
5 min read
In this program, all the subsets of the string need to be printed. The subset of a string is the character or the group of characters that are present inside the string. All the possible subsets for a string will be n(n+1)/2. For example, all possible subsets...
2 min read
In this program, we need to find out the minimum and maximum value node in the given singly linked list. We will maintain two variables min and max. Min will hold minimum value node, and max will hold maximum value node. In the above example, 1...
6 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, HCL, IBM, and Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are...
10 min read
In this program, we need to create a doubly linked list and rotate it by n node. This can be achieved by maintaining a pointer that starts from the head node and traverses the list until current points to the nth node. Move the list...
7 min read
Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program in different designs. To learn the pattern program, we must have...
15 min read
Program to print the elements of an array in reverse order In this program, we need to print the elements of the array in reverse order that is; the last element should be displayed first, followed by second last element and so on. Above array in reversed...
2 min read
Java Program to transpose matrix Converting rows of a matrix into columns and columns of a matrix into row is called transpose of a matrix. Let's see a simple example to transpose a matrix of 3 rows and 3 columns. public class MatrixTransposeExample{ public static void main(String args[]){ //creating a matrix int...
3 min read
Java Program to divide a string in 'N' equal parts. Here, our task is to divide the string S into n equal parts. We will print an error message if the string cannot be divisible into n equal parts otherwise all the parts need to be...
3 min read
Java Convert int to char We can convert int to char in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Here, the ASCII character of integer value will be stored in the char variable. To get the actual value in char...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India