Examples: 1 / \ 2 3 ==> 7 / \ / \ 4 5 6 7
Queue
to traverse all the nodes and find the maximum element.
1. Declare a variable to keep track of the current maximum value - Max
. Initialize it to the minimum Integer
value.
2. Create a Queue
and add the root node to it.
3. Loop till the Queue
becomes empty.
4. Within the Loop
, remove an element from the Queue
and compare the data with the current maxumum value. Update Max
if required.
5. If the element has left and the right nodes, insert those nodes into the queue.
6. Return the max variable
at the end of the Loop
.
public int findMaxItr(TreeNode root) { }
C
Java
Python