Recurisve:

/*
// 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> order = {};
if (root) {
traversal(root, order);
} return order;
} void traversal(Node* root, vector<int> &order) {
int num = root->children.size();
for (int i = 0; i < num; i++) {
traversal(root->children.at(i), order);
} order.push_back(root->val);
}
};

  

Iteratve:

/*
// 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> order = {};
map<Node*, bool> visit;
stack<Node*> nodeStack;
if (root) {
nodeStack.push(root);
} while (!nodeStack.empty()) {
Node* node = nodeStack.top();
int num = node->children.size();
if (num > 0 && visit.find(node) == visit.end()) {
for (int i = num - 1; i >= 0 ; i--) {
nodeStack.push(node->children.at(i));
}
visit[node] = true;
} else {
order.push_back(node->val);
nodeStack.pop();
}
} return order;
}
};

  

【leetcode】590. N-ary Tree Postorder Traversal的更多相关文章

  1. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  2. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  3. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  4. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  5. 【LeetCode】590. N-ary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 相似题目 参考资料 日期 题目地址:htt ...

  6. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  7. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  8. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  9. 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)

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

随机推荐

  1. SQL进阶-隐式类型转换

    一.隐式类型转换 1.隐式类型转换 隐式类型转换: '; 程序可读性差,且依赖数据库的隐式转换规则,如果数据库升级,则程序可能无法正确执行: 有可能会导致索引失效: 有可能会导致意想不到的结果: 显式 ...

  2. -bash: rvictls: command not found

    下载安装Command Line Tools for Xcodehttps://developer.apple.com/download/more/?name=for%20Xcode%20-# 显示包 ...

  3. myeclipse的安装与破解

    myeclipe安装和破解一直困扰我很长时间,我又是尴尬症的人,不破解就是不行,花费一天时间终于搞定是怎么破解的. 一:首先myeclipse的官方下载网站www.myeclipsecn.com/do ...

  4. webpack4.0中文文档踩坑记录

    一直没有正儿八经去看过webpack4.0的文档,前段时间工作比较轻松,于是就有了此文...面都这样一个问题:请问在您的开发生涯中,令你最痛苦最无奈的是什么?小生的回答只有一个:“阅读那些令人发指的文 ...

  5. mybatis之批量查询

    关于MyBatis批量更新和添加,参考我的如下文章即可:MyBatis的批量更新实例 MyBatis的批量添加实例 另外不管是批量的新增.删除.修改.查询也好,还是单个新增.删除.修改查询也罢.都会用 ...

  6. mysql优化查找执行慢的sql

    想要进行sql优化,肯定得先找出来需要优化的sql语句 一.mysql有一个自带的sql执行慢记录日志文件,所记录的日志取决于参数long_query_time控制,默认情况下long_query_t ...

  7. maven 左边空了

    看一下maven: 解决方法:进入maven的配置,把maven的路径配置一下,就好了: 结果:

  8. SAS 指定LOG LIST输出

    LIBNAME S '.\'; PROC PRINTTO LOG='.\LOG\PRINT_LOG.LOG';RUN; DATA A;SET SASHELP.CLASS (FIRSTOBS=2 OBS ...

  9. 信息系统项目十大管理ITO

    这是份关于信息系统项目管理师教程的内容提炼而成的电子文档,帮助所有备考信息系统管理师的考生准备,让大家快速记忆,助考加速.闲话少叙:直接上传我自己提炼的知识点.下图只是一部分,附件是所有内容.下载链接 ...

  10. 基于springboot+jquery+H5的文件(并发+断点+分片)的上传方案

    1.支持文件分片断点续传 2.支持已上传文件再次上传时秒传 3.多个人对同一个文件同时上传可以多线程并发协调上传,加快超大文件的上传速度. 技术点:springboot + webflux + red ...