Traverse Post Order
unknown
java
2 years ago
970 B
2
Indexable
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); } }
Editor is loading...