【题目】

给定一个 N 叉树,返回其节点值的前序遍历

例如,给定一个 3叉树 :

返回其前序遍历: [1,3,5,6,2,4]

【解析】

"""
# 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]
"""
ret[] #初始化结果数组
if root is None:return [] #树空的情况返回[],所以这里不能return None
#递归时候把该节点加入ret。 root相当于上层的x
#有个问题,每次递归会把ret置空,导致最后ret=[]
ret.append(root.val)
for x in root.children:
#这一步很重要,递归时把每一层的ret元素联合起来
ret.extend(self.preorder(x))
return ret

最新文章

  1. MySQL实现嵌套集合模型
  2. SharePoint 2013 状态机工作流之日常报销示例
  3. CSV的导入导出
  4. 2016国产恐怖惊悚《诡娃》HD720P.国语中字
  5. (三)stm32之串口通信DMA传输完成中断
  6. Binary to Text (ASCII) Conversion
  7. PPT 制作必备工具
  8. NFS挂载故障卡死的问题
  9. JavaScript(7)——事件2.0
  10. AutoTile 自动拼接(五) 学习与实践
  11. 添加Action View
  12. VisualStudio程序运行后控制台窗口一闪就没了
  13. JNI内存泄露JNI ERROR (app bug): local reference table overflow (max=512)
  14. linux查看防火墙的状态以及开启关闭
  15. spring cloud: Hystrix(六):feign的注解@FeignClient:fallbackFactory(类似于断容器)与fallback方法
  16. 839A Arya and Bran
  17. OAuth中client id的处理
  18. Linux内核设计笔记11——定时器
  19. [LeetCode]22. Generate Parentheses括号生成
  20. 哈工大LTP

热门文章

  1. Eclipse切换工作空间(Workspace)
  2. Confluence 6 WebDAV 禁用严格路径检查
  3. Confluence 6 附件是如何被索引的
  4. Selenium WebDriver中鼠标事件
  5. ionic3 点击input 弹出白色遮罩 遮挡上部内容
  6. Python获取当前时间及时间转换(datetime)
  7. mysql下载源码方法
  8. 函数sigsuspend
  9. Git 分支 - 分支的新建与合并
  10. Rookey.Frame企业级极速开发框架