data
to a list - Arraylist <Integer> preorderedList
. Return this list.Example:
1
/ \
2 3 ==> 1245367
/ \ / \
4 5 6 7
Data,Left,Right
ArrayList
to store the preordered list of elements and also a Stack
for storing the tree's nodes.root
is null
, return empty ArrayList
.root
into the Stack
.Stack
becomes empty.Stack
and push its data to the ArrayList
.null
, then push it to the Stack
.null
, then push it to the Stack
.ArrayList
.
public ArrayList<Integer> preorderItr(TreeNode root) { }
C
Java
Python