Java program to find the smallest element in a treeIn 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 |
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