Complete the code to visit the left child first in postorder traversal.
if (node.left != null) { [1]; }
In postorder traversal, we visit the left child first by calling postorder(node.left).
Complete the code to visit the right child second in postorder traversal.
if (node.right != null) { [1]; }
In postorder traversal, after visiting the left child, we visit the right child by calling postorder(node.right).
Fix the error in printing the root node's value in postorder traversal.
System.out.println([1]);To print the root node's value, use node.value. Printing node or its children will not print the value.
Fill both blanks to complete the recursive postorder traversal method.
public void postorder(Node node) {
if (node == null) return;
[1];
[2];
System.out.println(node.value);
}Postorder traversal visits left child first, then right child, then root. So call postorder(node.left) and postorder(node.right).
Fill all three blanks to create a postorder traversal that collects values in a list.
public void postorderCollect(Node node, List<Integer> result) {
if (node == null) return;
[1];
[2];
result.[3](node.value);
}remove instead of add on the list.To collect values in postorder, recursively call postorderCollect on left and right children, then add the root value to the list using add.
