ArrayList
which will be returned.
Example: 1 / \ 2 3 / \ / \ 4 5 6 7 Output => 4567231
1. If root
is null
, return.
2. Declare a Queue
and a Stack
.
3. Queue the root
.
4. Loop while the Queue
is not empty.
5. Within the loop, dequeue a node and insert its left
and right
nodes into the Queue
. At the same time, push
this element onto the Stack
.
6. Once the loop completes, pop
the Stack
's elements one by one and add them to the ArrayList
.
public ArrayList<Integer> levelorderRev(TreeNode root) { }
C
Java
Python