Print a BST in Post-Order
Print a BST in Post-Order
Complete the private helper postOrder(Node node, StringBuilder sb) so that postOrder() returns every value in the tree, in post-order, separated by spaces.
Reminder: post-order traversal. Post-order visits the left subtree, then the right subtree, and the node itself LAST. Every node is visited only after everything below it, so the leaves come out first and the root always comes out at the very end.
8
/ \
3 10
/ \ \
1 6 14For this tree post-order visits 1 6 3 14 10 8. Children before their parent is exactly the order you need when a parent depends on its children being finished first, for example taking a tree apart (you cannot remove a parent before its children) or adding up the sizes of folders inside folders.
Requirements:
1. When node is null, return straight away.
2. Otherwise traverse node.left, then traverse node.right, and call visit(node, sb) last.
3. visit already adds the value with a single space between values.
4. Use recursion, not a loop.
The Node class is provided - do not rename it and do not add public to it, because a Java source file may contain only one public class.
Do NOT write a main method.
Expected Output:
Post-order: [20 40 30 80 70 50] Post-order: [20 40 30 60 80 70 50]
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...