590. N-ary Tree Postorder Traversal*
590. N-ary Tree Postorder Traversal*
https://leetcode.com/problems/n-ary-tree-postorder-traversal/
题目描述
Given an n-ary tree, return the postorder traversal of its nodes’ values.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
Follow up:
Recursive solution is trivial, could you do it iteratively?
Example 1:
Input: root = [1,null,3,2,4,null,5,6]
Output: [5,6,3,2,4,1]
Example 2:
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
Constraints:
- The height of the
n-ary
tree is less than or equal to 1000 - The total number of nodes is between
[0, 10^4]
C++ 实现 1
递归的方式.
/*
// Definition for a Node.
class Node {
public:int val;vector<Node*> children;Node() {}Node(int _val) {val = _val;}Node(int _val, vector<Node*> _children) {val = _val;children = _children;}
};
*/
class Solution {public:vector<int> postorder(Node* root) {if (!root) return {};vector<int> res;for (auto &c : root->children) {auto tmp = postorder(c);std::copy(tmp.begin(), tmp.end(), back_inserter(res));}res.push_back(root->val);return res;}
};
C++ 实现 2
迭代的方式. 迭代的方式可以仔细研究一下, children 按顺序入栈, 最后 reverse res
.
class Solution {public:vector<int> postorder(Node* root) {if (!root) return {};vector<int> res;stack<Node*> st;st.push(root);while (!st.empty()) {auto r = st.top();st.pop();res.push_back(r->val);for (auto &c : r->children) st.push(c);}std::reverse(res.begin(), res.end());return res;}
};
590. N-ary Tree Postorder Traversal*相关推荐
- 5. Binary Tree Postorder Traversal
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 【二叉树的迭代版后序遍历】LeetCode 145. Binary Tree Postorder Traversal
LeetCode 145. Binary Tree Postorder Traversal Solution1:递归版答案 二叉树的后序遍历递归版是很简单的,关键是迭代版的代码既难理解又难写!但听了花 ...
- [LeetCode] N-ary Tree Postorder Traversal N叉树的后序遍历
Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...
- Binary Tree Postorder Traversal
https://leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return the posto ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- LeetCode 145. Binary Tree Postorder Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-postorder-traversal/ 题目: Given a binary tree, retu ...
- 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- C#LeetCode刷题之#590-N叉树的后序遍历(N-ary Tree Postorder Traversal)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4092 访问. 给定一个 N 叉树,返回其节点值的后序遍历. 例如 ...
- 【leetcode】590. N-ary Tree Postorder Traversal
题目如下: 解题思路:凑数题+2,做完先序做后序.凑数博+2. 代码如下: class Solution(object):def postorder(self, root):""& ...
最新文章
- 电脑显示未安装任何音频输出设备_一套完整的台式电脑有哪些配置
- python 批量下载网址_python 遍历oss 实现批量下载
- iOS 通知观察者的被调函数不一定运行在主线程
- android使用软引用构建缓存
- Ubuntu 压缩解压汇总(自己常用+持续更新)
- python3连接mysql数据库_python3.4连接mysql数据库
- Codeforces Round #694 (Div. 2) D. Strange Definition 质因子分解 + 平方数
- 49session的生命周期实例
- Android手势的识别
- SharePoint 2010学习资源
- 源码安装MySQL步骤
- 六石风格:雨伞用袋子套起来
- 线性反馈移位寄存器(LFSR)
- JavaScript实现点击一下显示,再点击一下隐藏的功能(使用工厂函数)
- 利用华为手机给台式机提供网络
- JavaWeb-文件上传和下载
- Ps做的图片html显示不了,为什么导入到PS的图片有图层却显示不出来?
- 有一个棋盘,有64个方格,在第一个方格里面放1粒芝麻重量为0.00001,第二个里面放2粒,第三个里面放4,第四个8 ,以此类推,棋盘上放的所有芝麻的重量?
- 如何利用工具低成本构建腾讯云灾备方案?
- 括号匹配(动态规划)
热门文章
- python车牌识别系统开源代码_python+opencv实现车牌定位功能(实例代码)
- 二叉树之前序遍历、中序遍历、后续遍历
- 计算机可以唱什么音乐,5款经典的可以学习唱歌的app介绍
- three.js 07-06 之 Sprite 一幅图片多个精灵
- 九大常用设计模式学习-装饰者模式
- 中科院oracle,《Oracle 10G 系统教程 中科院培训老师讲授》
- 杭电oj第1000题—— A + B Problem
- 紫外线传感器 韩国GENICOM进口 GUVA-T11GD-L 全波段检测,原厂渠道
- C语言统计数字出现的次数
- 爬虫中常见的反爬手段和解决方法