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. 单点登录的实践demo

    https://github.com/deadzq/web-sso-server 统一认证中心 https://github.com/deadzq/web-system-client1 用户客户端 结 ...

  2. 判断一个数是否能整开方,perfect square

    int m,n;m=sqrt(n);m*m==n?yes:no; https://www.codewars.com/kata/56269eb78ad2e4ced1000013/solutions/ja ...

  3. 原生js如何判断元素出现在可视区

    元素出现在可视区 scorll滑动的距离>=当前元素距离浏览器最顶端的高度+当前元素自身的高度-当前可视区的高度 触底 scorll滑动的距离>=当前scroll总高度-当前可视区的高度

  4. luoguP4169 [Violet]天使玩偶/SJY摆棋子 K-Dtree

    P4169 [Violet]天使玩偶/SJY摆棋子 链接 luogu 思路 luogu以前用CDQ一直过不去. bzoj还是卡时过去的. 今天终于用k-dtree给过了. 代码 #include &l ...

  5. 【数论】[因数个数]P4167樱花

    题目描述 求不定方程 \(\frac {1}{x} + \frac{1}{y} = \frac{1}{n!}\)的正整数解的个数 \(n \leq 100^6\) Solution 化简得 \(x * ...

  6. TypeError: BlobBuilder is not a constructor

    var BlobBuilder = window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window. ...

  7. 二分法递归版本(c++)

    利用二分法求解在区间[0,π/2]上的根 #include<iostream> #include <cmath> using namespace std; double dic ...

  8. 歪国人整理的 2019 年 Java 开发路线图,值得参考!

      许多Java开发人员都希望通过某种Java成长路线图,来解答有关:该学习哪些技术,使用哪些工具以及框架之类的问题. 在此,我将向大家展示一张根据自己多年经验总结出的路线图.该路线图在保持简单可行的 ...

  9. chrome如何禁用js代码

    chrome如何禁用js代码 一.总结 一句话总结: 设置-->高级-->隐私设置和安全性-->网站设置-->javascript 中禁止javascript即可 二.chro ...

  10. Re3 : Real-Time Recurrent Regression Networks for Visual Tracking of Generic Objects

    Re3 : Real-Time Recurrent Regression Networks for Visual Tracking of Generic Objects 2019-10-04 14:4 ...