Postorder traversal
- Visit all the nodes in the left subtree
- Visit all the nodes in the right subtree
- Visit the root node
When to use? You can use post-order traversal to delete a tree, since you can delete all of a node's children before itself.
SNIPPET
1postorder(root.left)
2postorder(root.right)
3display(root.data)

xxxxxxxxxx
27
function postorderTraversal(root) {
let res = [];
helper(root, res);
return res;
}
​
function helper(root, res) {
if (!root) {
return res;
}
helper(root.left, res);
helper(root.right, res);
res.push(root.val);
return res;
}
​
const root = {
val: 1,
left: {
val: 2
},
right: {
val: 3
}
};
​
console.log(postorderTraversal(root));
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment