Java program to find the smallest element in a tree
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.
a. smallestElement() will find out the smallest node in binary tree:
Smallest element in the binary tree: 1