Created
February 22, 2021 10:22
-
-
Save ficapy/f587efea9f0226dc8508118201fbc8a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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