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. ABP 基本操作

    基本信息: 官网:https://aspnetboilerplate.com api:https://aspnetboilerplate.com/Pages/Documents/Articles-Tu ...

  2. Markdown&Latex学习笔记,qwq

    目录 推荐的文章 居中 字体 加颜色 指数 分数 根号 神奇的符号(不要多想qwq) 箭头 小于号 大括号 累加符号 累乘符号 下标 \(\phi\)&\(\varphi\) \(\equiv ...

  3. HustOJ二次开发之修改数据库连接池

    有的时候我们会因为某种业务需要的情况下,需要修改hustoj默认的数据库连接池之类的. 修改数据库连接池步骤 进入到对应的目录 /home/judge/src/web/include 找到db_inf ...

  4. 数据结构(一)二叉树 & avl树 & 红黑树 & B-树 & B+树 & B*树 & R树

    参考文档: avl树:http://lib.csdn.net/article/datastructure/9204 avl树:http://blog.csdn.net/javazejian/artic ...

  5. Spring 注解@Value详解

    一.spring(基础10) 注解@Value详解[1] 一 配置方式 @value需要参数,这里参数可以是两种形式: [html] view plain copy @Value("#{co ...

  6. Netty 优雅退出

    Netty 优雅退出机制和原理:https://www.infoq.cn/article/netty-elegant-exit-mechanism-and-principles/?utm_source ...

  7. 【转】Android 破解视频App去除广告功能详解及解决办法总结

    Android 破解视频App去除广告功能 作为一个屌丝程序猿也有追剧的时候,但是当打开视频app的时候,那些超长的广告已经让我这个屌丝无法忍受了,作为一个程序猿看视频还要出现广告那就是打我脸,但是我 ...

  8. MyBatis(十一):Mybatis 动态SQL语句完成多条件查询

    之前文章中对in的用法做过讲解:<MyBatis(四):mybatis中使用in查询时的注意事项> 实际上对于多个参数的用法也是这是注意的: 多参&if判空&List集合判 ...

  9. 韦东山视频第3课第2节_JNI_C调用JAVA_P【学习笔记】

    C调JAVA方法主要步骤如下: 一.C代码调用java的静态方法 Hello.java public class Hello{ public static void main(String args[ ...

  10. 福昕PDF高级企业版编辑器9.5 Foxit PhantomPDF Business安装破解教程

    title: "福昕PDF高级企业版编辑器9.5 Foxit PhantomPDF Business安装破解教程" categories: soft tags: soft auth ...