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?

---------------------------------------------------------------------------------------------------------------------------------

额,这个迭代不会,不过,如果会了二叉树的前序遍历的递归解法的话,解这个题就会感觉简单了,同理,后序遍历也是,不过,中序遍历上可能会很难写。

C++代码:

/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<int> preorder(Node* root) {
vector<int> res;
helper(root,res);
return res;
}
void helper(Node *root,vector<int> &res){
if(!root) return;
res.push_back(root->val);
for(Node* cur : root->children){
helper(cur,res);
}
}
};

(N叉树 递归) leetcode589. N-ary Tree Preorder Traversal的更多相关文章

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

    589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...

  2. (N叉树 递归) leetcode 590. N-ary Tree Postorder Traversal

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  3. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  4. (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

  5. [LeetCode] N-ary Tree Preorder Traversal N叉树的前序遍历

    Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary  ...

  6. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  7. 【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】

    [144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...

  8. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  9. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

  10. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

随机推荐

  1. “宇宙最强” IDE,Visual Studio 2019 正式发布

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 本文由葡萄城翻译并发布 今天凌晨Visual Studio 2019已经正式发布,现在已经可以下载了.使用V ...

  2. 【Linux】【MySQL】CentOS7、MySQL8.0.13 骚操作速查笔记——专治各种忘词水土不服

    1.前言 [Linux][MySQL]CentOS7安装最新版MySQL8.0.13(最新版MySQL从安装到运行) 专治各种忘词,各种水土不服. - -,就是一个健忘贵的速查表:(当然不包括SQL的 ...

  3. RowKey设计之单调递增行键/时序数据

    ​在一个集群中,一个导入数据的进程锁住不动,所有的client都在等待一个region (因而也就是一个单个节点),过了一会后,变成了下一个region…​如果使用了单调递增 或者时序的key便会造成 ...

  4. 神经网络MPLClassifier分类

    代码: # -*- coding: utf-8 -*- """ Created on Fri Aug 24 14:38:56 2018 @author: zhen &qu ...

  5. nginx常用场景

    1.浏览器缓存 server { listen 8083; server_name 127.0.0.1; sendfile on; access_log /var/log/nginx/static_s ...

  6. 多租户通用权限设计(基于casbin)

    多租户通用权限设计(基于 casbin) 所谓权限控制, 概念并不复杂, 就是确认某个操作是否能做, 本质上仅仅就是个bool判断. 权限几乎是每个系统必不可少的功能, 和具体业务结合之后, 在系统中 ...

  7. MyBatis学习日记(二): MyBatis Say Hello

    首先在Eclipse中创建一个maven工程: 在maven工程下的pom.xml文件中添加MyBatis.MySQL.Junit依赖: <project xmlns="http:// ...

  8. ASP.NET Core RSA加密或解密

    前言 这两天主要是公司同事用到了RSA加密,事后也看了下,以为很简单,最终利用RSACryptoServiceProvider来实现RSA加密,然后大致了解到RSACryptoServiceProvi ...

  9. 使用 ng build 构建后资源地址引用错误的问题

    最近使用 Angular 做项目的时候,通过 ng build 打包后的资源地址都带有 localhost:4200,百思不得其解,以为是打包的问题. 最后在 index.html 的文件中发现有一个 ...

  10. 类 Calendar

    简介 Java.util.Calendar是日历类,在Date后出现,替换掉了许多Date的方法.该类将所有可能用到的时间信息封装为静态成员变量,方便获取.日历类就是方便获取各个时间属性的.注意Cal ...