Given an n-ary tree, return the preorder traversal of its nodes' values.

For example, given a 3-ary tree:

Return its preorder traversal as: [1,3,5,6,2,4].

Note:

Recursive solution is trivial, could you do it iteratively?

题解:

class Solution {
public:static void preOrder(Node* root, vector<int> &ans) {if (root != NULL) {ans.push_back(root->val);for (int i = 0; i < root->children.size(); i++) {preOrder(root->children[i], ans);}    }}vector<int> preorder(Node* root) {vector<int> ans;preOrder(root, ans);return ans;}
};

LeetCode-589. N-ary Tree Preorder Traversal相关推荐

  1. 【二叉树迭代版前序遍历】LeetCode 144. Binary Tree Preorder Traversal

    LeetCode 144. Binary Tree Preorder Traversal Solution1:递归版 二叉树的前序遍历递归版是很简单的,前序遍历的迭代版相对是最容易理解的. 迭代版链接 ...

  2. LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium

    题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode* ...

  3. [Lintcode]66. Binary Tree Preorder Traversal/[Leetcode]144. Binary Tree Preorder Traversal

    66. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal 本题难度: Easy/Medium Topic: Bina ...

  4. 14 二叉树的前序遍历(Binary Tree Preorder Traversal)

    文章目录 1 题目 2 描述 3 解决方案 3.1 递归算法 3.1.1 遍历法(Traverse) 思路 源码 3.1.2 分治法(Devide And Conquer) 思路 源码 3.2 非递归 ...

  5. [LeetCode] 589. N-ary Tree Preorder Traversal

    原题链接:https://leetcode.com/problems/n-ary-tree-preorder-traversal/ 1. 题目介绍 Given an n-ary tree, retur ...

  6. leetcode 589. N-ary Tree Preorder Traversal

    一题目 Given an n-ary tree, return the preorder traversal of its nodes' values. Nary-Tree input seriali ...

  7. [LeetCode 题解]: Binary Tree Preorder Traversal

    前言 [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a binary ...

  8. 589. N 叉树的前序遍历(javascript)589. N-ary Tree Preorder Traversal

    给定一个 n 叉树的根节点 root ,返回 其节点值的 前序遍历 . n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例). Given the root of ...

  9. [leetcode] Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  10. leetcode 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

最新文章

  1. nginx tomcat https
  2. Java文件操作大全(绝对的经典,值得收藏!)
  3. Android开发之Mac配置环境变量
  4. Maven 使用bat批量清除本地仓库的lastUpdated文件
  5. python json删除字段_如何使用python删除json对象?
  6. 【软考软件评测师】2016综合知识历年真题
  7. 数据库管理系统的未来是什么?
  8. 日常生活开支记账明细_你的理财小管家!简单明了!皮面理财记账本明细流水支出笔记本!...
  9. 用Python中的VTK库导入并显示Assembly的STL文件
  10. 小米Redmi Airdots无线蓝牙耳机无法串联问题解决
  11. 线程池使用不当导致系统假死
  12. PDP Context 激活流程(UE发起)
  13. NCE4 L11 How to grow old
  14. 服务器cpu型号各个数字,服务器cpu型号 数字
  15. 四大垃圾回收算法七大垃圾回收器
  16. hc sr501 c语言程序,HC-SR501人体红外感应模块资料汇总(原理图、常见问题、程序等)...
  17. java线程池介绍(一)
  18. 用计算机程序计算兴利库容,《水利计算》课程教学大纲
  19. 牛客2019湘潭大学程序竞赛
  20. python创建学生字典_Python创建字典的八种方式

热门文章

  1. Centos7配置阿里云DNS
  2. LeetCode 力扣每日一题 488.祖玛游戏
  3. linux surface pro 4 driver,重置出错?微软Win10平板Surface Pro 4重装系统教程详解
  4. EV1527离线语音控制器模块调试日志
  5. 记一次linux redhat 7.4 maipo单用户模式恢复文件
  6. 可能是macOS下最好的微博客户端Maipo
  7. Kafka牛逼在哪里?
  8. excel取消保护(忘记密码)
  9. xp系统修复 sfc /scannow 免光盘技巧
  10. 2021Java最新真实面试题汇总(持续更新)