Java program to find the smallest element in a tree17 Mar 2025 | 3 min read In this program, we will find out the smallest node in the given binary tree. We first define variable min that will hold root's data. Then, we traverse through left sub-tree to find the smallest node in left subtree. Compare it with min and store minimum of two in variable min. Then, we traverse through right subtree to find smallest node and compare it with min. At the end, min will have the smallest node. ![]() Above diagram represents a binary tree. Initially, min will hold 4. Recursive through left subtree. Recursive through right subtree. Recurse in right subtree of 3 So, smallest node in above binary tree is 1. Algorithm
a. smallestElement() will find out the smallest node in binary tree:
Program:Output: Smallest element in the binary tree: 1 Next TopicJava Programs |
It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Adobe, Apple, Infosys, 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,...
5 min read
In this program, we will create a circular linked list and sort the list in ascending order. In this example, we maintain two nodes: current which will point to head and index which will point to node to current. The first loop, keep track...
3 min read
In this program, we will create a singly linked list and delete a node from the middle of the list. To accomplish this task, we will calculate the size of the list and then divide it by 2 to get the mid-point of the list....
5 min read
In this program, we need to replace all the spaces present in the string with a specific character. String: Once in a blue moon String after replacing space with '-': Once-in-a-blue-moon One of the approach to accomplish this is by iterating through the string to find spaces. If...
2 min read
In this program, we will create a circular linked list and print all the nodes present in the list. Circular Linked List: The circular linked list is a kind of linked list. First thing first, the node is an element of the list, and it has two...
4 min read
Matrix Programs Matrix relates to mathematics that can be defined as a 2-dimensional array in the form of a rectangle which is filled either with numbers or symbols or expressions as its elements. The matrix has a row and column arrangement of its elements. A matrix with...
1 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, 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 going to solve...
13 min read
In this program, we need to check whether a string is a rotation of another string or not. String 1: abcde String 2: deabc String 1 + String 1: abcdeabcde Consider the above example, suppose we need to check whether string 2 is a rotation of string 1. To...
2 min read
In this program, we need to search a given node in a doubly linked list. To solve this problem, we will traverse through the list using a node current. Current points to head and start comparing searched node data with current node data. If they are...
5 min read
Java Convert Decimal to Octal We can convert decimal to octal in java using Integer.toOctalString() method or custom logic. Java Decimal to Octal conversion: Integer.toOctalString() The Integer.toOctalString() method converts decimal to octal string. The signature of toOctalString() method is given below: public static String toOctalString(int decimal) Let's see the simple example...
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