PEP-8
As commented by greybeard, the PEP-8 guidelines for Python suggest using CapWords
for class names, and snake_case
for variables and method names. So postOrderTraversal
should be post_order_traversal
Type Hints
Your code is incomplete, which makes it more difficult to review. Clearly, you have defined the TreeNode
elsewhere, or your code would not run. You should include that code in your question post. (But don’t change it now that there have been answers posted to your question; your change would just be rolled back. In future questions, include all referenced code, including import statements and type definitions.)
You are only half using type hints. You indicate that postOrderTraversal
takes a TreeNode
as an input parameter, but not specified what it returns. It doesn’t return anything, which should be indicated with -> None
. Eg)
def postOrderTraversal(root: TreeNode) -> None:
# remainder of function
Documentation
You should help future users of your function understand what your function does and how to use it properly, without needing to read the code, by providing a """docstring"""
at the start of the function. A user may then type help(postOrderTraversal)
, which would print out that information.
You should help future maintainers of the code (including yourself) understand how the code works by including comments in the code, to describe the overall algorithm, and any non-obvious aspects. It is clear Charles Merriam did not understand the point of checking if popped == stack[-1].right
, so that could definitely stand a comment or two.
Please note the distinction between “users of” and “maintainers of”. A user needs a docstring describing what the function does, what it needs and what it returns. A maintainer needs comments in the code describing how the code functions.
Iterative Algorithm
When you first visit a node, you push it on the stack, so you can return to it a second time, and then proceed to walk down the left branch.
When you return to the node, you leave it on the stack, so you can return to it a third time, and then you proceed to walk down the right branch.
When you again return to the node the third time, you finally “process” the node ... by popping it off the stack and printing its value.
You maintain a state machine using curr
, popped
and stack[-1].right
to determine whether this is the first, second, or third visit to the node, which is somewhat complicated, despite you and your wife finding it “easy to grasp.”
Instead, consider pushing both the node and the state information onto the stack.
stack.append((curr, False))
curr = curr.left
And pop both pieces off the stack:
curr, visited_right = stack.pop()
if visited_right:
print(curr.val)
curr = None
else:
stack.append((curr, True))
curr = curr.right
With this change, you no longer need to maintain and check popped
against stack[-1].right
. The state is stored along with the node on the stack.
stack
, indexable by integers, holding things with attributesright
andval
, the latter getting printed conditionally. Now, what is thatpostOrder
as inpostOrderTraversal
? (Can't be Python, where identifiers should besnake_case
.) \$\endgroup\$