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

For example, given a 3-ary tree:

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

Note:

Recursive solution is trivial, could you do it iteratively?

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

leetcode589. N-ary Tree Preorder Traversal类似。用递归会简单些

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> postorder(Node* root) {
vector<int> vec;
helper(root,vec);
return vec;
}
void helper(Node* root,vector<int> &vec){
if(!root) return;
for(Node *cur:root->children){
helper(cur,vec);
}
vec.push_back(root->val);
}
};

(N叉树 递归) leetcode 590. N-ary Tree Postorder Traversal的更多相关文章

  1. LeetCode 590. N叉树的后序遍历(N-ary Tree Postorder Traversal)

    590. N叉树的后序遍历 590. N-ary Tree Postorder Traversal 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. LeetCode590. N-ary Tre ...

  2. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  3. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  4. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  5. LeetCode解题报告:Binary Tree Postorder Traversal

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

  6. LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)

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

  7. LeetCode OJ 145. Binary Tree Postorder Traversal

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

  8. LeetCode算法题-N-ary Tree Postorder Traversal(Java实现)

    这是悦乐书的第269次更新,第283篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第136题(顺位题号是590).给定一个n-ary树,返回其节点值的后序遍历.例如,给定 ...

  9. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

随机推荐

  1. Odoo 10的Linux安装

    CentOS7安装Odoo10流程如下一.更新系统#yum clean all#yum update 二.安装 PostgreSQL 1.安装数据库#yum install postgresql po ...

  2. 一起学Android之ViewPager

    本文以一个简单的小例子,简述在Android开发中ViewPager的常见用法,仅供学习分享使用. 概述 ViewPager是一个支持使用者左右滑动的布局管理控件,可以通过一个实现的(适配器)Page ...

  3. MyDAL - 快速使用

    索引: 目录索引 一.安装 在 VS 中执行一下 package 命令: PM> Install-Package MyDAL 二.API-快速使用 1.命名空间,只需: using MyDAL; ...

  4. Git-初始化配置及SSH_key配置

    step1.安装完Git,执行检查是否安装成功:git --version step2.配置全局变量 配置完执行检查:git config --list step3.生成SSH_KEY 如果报ssh- ...

  5. Winserver-默认以管理员运行程序

    打开secpol.msc 打开本地安全策略找到安全设置--本地策略--安全选项用户账户控制:以管理员批准模式运行所有管理员---改为禁用保存设置重启电脑

  6. Delphi 数据转换

    指针转换   Pointer——string string:=PChar(Pointer);{ Pointer指向的数据要以#0结尾.使用System.AllocMem(Size)分配的内存是用#0填 ...

  7. RobotFramework第一篇之环境搭建

    定义:是一款python编写的功能自动化测试框架,具备良好的扩展性,可以进行分布性测试 1:对编程能力要求低,容易上手 2:关键字调用方式,已经定义好的功能,只需要去调用它,一个关键字实现了一个功能, ...

  8. i春秋misc部分writeup

    i春秋misc部分writeup 一.敲击 方方格格,然后看到下面的格式,猜测出是键盘上的布局,然后看这些字母形成的形状想那些字母,就是flag了 2.滴滴滴 放到ctfcack里解密,发现时栅栏密码 ...

  9. Java基础系列--08_集合1

    ---恢复内容开始--- 集合当中有很多都是应用到泛型的技术,所以在讲集合之前,应该先将泛型的概念普及一下. 泛型:    (1)泛型是一种类型,但是这种类型是在编译或者调用方法时才确定.    (2 ...

  10. HTML基础-------最初概念以及相关语法

    HTML概念以及相关语法 HTML HTML是一种类似于(c,java,c++)之类的语言,他是用来描述网页的一种语言.通过各种标签所代表的语义来构建出一个网页,再通过浏览器的渲染功能来实现该网页的各 ...