Given a binary tree. The task is to find out the maximum and minimum element in a binary tree without using recursion or stack or queue i.e, space complexity should be O(1).
Input : 12 / \ 13 10 / \ 14 15 / \ / \ 21 24 22 23 Output : Max element : 24 Min element : 10 Input : 12 / \ 19 82 / / \ 41 15 95 \ / / \ 2 21 7 16 Output : Max element : 95 Min element : 2
Prerequisite : Inorder Tree Traversal without recursion and without stack
1. Initialize current as root
2. Take to variable max and min
3. While current is not NULL
- If the current does not have left child
- Update variable max and min with current’s data if required
- Go to the right, i.e., current = current->right
Make current as the right child of the rightmost
node in current’s left subtree
- Go to this left child, i.e., current = current->left
- Make current as the right child of the rightmost
Below is the implementation of the above approach :
Max Value is : 25 Min Value is : 3
Space complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Postorder traversal of Binary Tree without recursion and without stack
- Inorder Tree Traversal without recursion and without stack!
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Decimal to Binary using recursion and without using power operator
- Find the node with maximum value in a Binary Search Tree using recursion
- Find Maximum Level Sum in Binary Tree using Recursion
- Find the node with minimum value in a Binary Search Tree using recursion
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Inorder Tree Traversal without Recursion
- Preorder Traversal of N-ary Tree Without Recursion
- Level order traversal in spiral form | Using one stack and one queue
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Sum and Product of maximum and minimum element in Binary Tree
- Sum and Product of minimum and maximum element of Binary Search Tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Zig-Zag traversal of a Binary Tree using Recursion
- Bottom View of a Binary Tree using Recursion
- Right view of Binary Tree using Queue
- Print root to leaf paths without using recursion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.