data
to the list - Arraylist <Integer> preorderedList
.
Example: 1 / \ 2 3 ==> 1245367 / \ / \ 4 5 6 7
data
of the current node to the Arraylist. left
subtree by recursively calling the preorder
method. right
subtree by recursively calling the preorder
method.
add
to add the node's data to the ArrayList<Integer>
.
//Populated the elements of the tree in the below list in preorder format ArrayList<Integer> preorderedList = new ArrayList<Integer>(); public void preorder(TreeNode root) { }
C
Java
Python