leetcode 589. N 叉树的前序遍历

递归 前序遍历

class Solution:
    def preorder(self, root: 'Node') -> List[int]:
        ans = []

        def dfs(root):
            if not root:
                return
            ans.append(root.val)
            for node in root.children:
                dfs(node)
        
        dfs(root)
        return ans

猜你喜欢

转载自blog.csdn.net/milk_paramecium/article/details/122850720