Arraylist <Integer> postorderedList
with the nodes of the tree in postorder, recursively.
Example: 1 / \ 2 3 ==> 4526731 / \ / \ 4 5 6 7
left
subtree by recursively calling the postorder function. right
subtree by recursively calling the postorder function.data
part of root element(or current element) in the arraylist
add
method to add the node's data in the postorderedList list
1. Starting with the root node, recurse through the left nodes
. Data of the elements in the left subtree will get added to the ArrayList.
2. Recurse through the right nodes
. Data of the elements in the right subtree will get added next into the ArrayList.
3. Add the data of the root nodes
in the Array List then return from the method
//Populate the elements of the tree in the below list in the post order way. ArrayList<Integer> postOrderedList = new ArrayList<Integer>(); public void postorder(TreeNode root) { }
C
Java
Python