一、题目描述
给定一个 N 叉树,返回其节点值的前序遍历
例如,给定一个3叉树:
""" # Definition for a Node. class Node(object): def __init__(self, val, children): self.val = val self.children = children """ class Solution(object): def preorder(self, root): """ :type root: Node :rtype: List[int] """ if not root: return [] result = [root.val] for leaf in root.children: result += self.preorder(leaf) return result
原文地址:https://www.cnblogs.com/always-fight/p/10337242.html
时间: 2024-10-01 05:16:39