Description

Postorder traversal, is the traversal of a binary tree where we count the left node, then the right node, then the node itself last.
?

Reach for when

You care about bottom-up computation - the node comes after both subtrees, so a node can use its children's results.

Runtime

O(n)

Visualization

Binary Tree-1784008572088.webp217

Pseudocode

PostOrder(node):
	if node is null:
		return
	PostOrder(node.left)
	PostOrder(node.right)
	save node.value

Code

def traverse(node, answer=[]):
	if not node:
		return answer

	traverse(node.left, answer)
	traverse(node.right, answer)
	answer.append(node.value)
	return answer