python 类中的递归函数使用

📅 2026/7/28 18:21:37 👁️ 阅读次数 📝 编程学习
python 类中的递归函数使用

n叉数的前序遍历

class Solution: def preorder(self, root: 'Node') -> List[int]: order=[] if root!=None: order.append(root.val) for i in range(len(root.children)): node=root.children[i] order+=self.preorder(node) return order

类中的函数递归调用,要使用self.preorder()调用