(node)
| 137 | |
| 138 | |
| 139 | def post_order_iter(node): |
| 140 | if not isinstance(node, TreeNode) or not node: |
| 141 | return |
| 142 | stack1, stack2 = [], [] |
| 143 | n = node |
| 144 | stack1.append(n) |
| 145 | while stack1: # to find the reversed order of post order, store it in stack2 |
| 146 | n = stack1.pop() |
| 147 | if n.left: |
| 148 | stack1.append(n.left) |
| 149 | if n.right: |
| 150 | stack1.append(n.right) |
| 151 | stack2.append(n) |
| 152 | while stack2: # pop up from stack2 will be the post order |
| 153 | print(stack2.pop().data, end=" ") |
| 154 | |
| 155 | |
| 156 | if __name__ == '__main__': |
no test coverage detected