LeetCode Algorithm 589. N 叉树的前序遍历
589. N 叉树的前序遍历
Ideas
二叉树的前序遍历模板,拿过来稍微一改就完事了。
def preorderTraversalLoop(node):
if not node:
return
stack = [node] # list 模拟 stack
while stack:
tmp = stack.pop()
print(tmp.value, end=' ')
if tmp.right:
stack.append(tmp.right)
if tmp.left:
stack.append(tmp.left)
Code
Python
from typing import List
# Definition for a Node.
class
共有 0 条评论