Skip to content

Instantly share code, notes, and snippets.

@ficapy
Created February 22, 2021 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ficapy/f587efea9f0226dc8508118201fbc8a9 to your computer and use it in GitHub Desktop.
Save ficapy/f587efea9f0226dc8508118201fbc8a9 to your computer and use it in GitHub Desktop.
def traverse(root):
stack, ret = [root], []
while stack:
cur = stack.pop()
if isinstance(cur, Node):
# stack.extend([cur.right, cur.left, cur.val]) # <- 前序遍历
# stack.extend([cur.right, cur.val, cur.left]) # <- 中序遍历
stack.extend([cur.val, cur.right, cur.left]) # <- 后序遍历
elif isinstance(cur, int):
ret.append(cur)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment