145. Binary Tree Postorder Traversal
# Hard, DFS
Solution 1: Recursive
Same logic as inorder and preorder binary traversal.
Solution 2: Divide & Conquer
Same logica as preorder and inorder binary traversal.
Solution 3. Iterative(While-loop)
Use while-loop to find the most left node, adding them into the stack , remark those nodes as False at the same time( False means this node' right subtree hasn't been visited). Pop the most left node, if the node has been marked as True(the right subtree has been visited), then this node is root, add to res and put current ptr to None(in order to get parent node). Otherwise mark this node as visited, then push all the right subtree into the stack.
This is most efficient method.
Last updated
Was this helpful?