Traverse Post Order

mail@pastecode.io avatar
unknown
java
a year ago
970 B
0
Indexable
Never
package LAB4;

import java.util.ArrayList;

public class traversePostOrder
{
    // Declare an ArrayList to store the traversal results
    private ArrayList<Integer> results;

    // Constructor for the traversePostOrder class
    traversePostOrder(Node currentNode, ArrayList<Integer> results)
    {
        // Set the results instance variable to the passed-in ArrayList
        this.results = results;

        // If the current node has a left child, recursively traverse the left subtree
        if (currentNode.left != null)
        {
            new traversePostOrder(currentNode.left, results);
        }

        // If the current node has a right child, recursively traverse the right subtree
        if (currentNode.right != null)
        {
            new traversePostOrder(currentNode.right, results);
        }

        // Add the current node's value to the results ArrayList after traversing its subtrees
        results.add(currentNode.value);
    }
}