您当前的位置: 首页 >  leetcode

LeetCode Algorithm 589. N 叉树的前序遍历

发布时间:2022-02-09 21:27:09 ,浏览量:0

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 Node: def __init__(self, val=None, children=None): self.val = val
        self.children = children class Solution: def preorder(self, root: 'Node') -> List[int]: if not root: return [] stack, ans = [root], [] while stack: item = stack.pop() ans.append(item.val) for i in range(len(item.children) - 1, -1, -1): stack.append(item.children[i]) return ans
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    108697博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.1330s