0145. Postorder Traversal
Easy | Tree + Traversal | 20 ms (99.24%), 14.1 MB (91.99%)
Source: LeetCode - Binary Tree Postorder Traversal GitHub: Solution / Performance
Given the root
of a binary tree, return the postorder traversal of its nodes' values.
Postorder traversal stops iterating when there is no left-child node.
In other words, we need to keep non-visited nodes along the path.
Pop the stack to retrieve the tuple/pair with the current node and visited status.
If already being visited, we could append the node's value to the answer.
If not:
First, push the current node but with visited status
Second, push the right-child node with non-visited status (if the node exists)
Last, push the left-child node with non-visited status (if the node exists)
Last updated