【leetcode】589. N-ary Tree Preorder Traversal
题目:
Given an n-ary tree, return the preorder traversal of its nodes' values.
For example, given a 3-ary tree:

Return its preorder traversal as: [1,3,5,6,2,4].
Note:
Recursive solution is trivial, could you do it iteratively?
/*
// 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> preorder(Node* root) {
vector<int> order = {};
if (root) {
traversal(root, order);
} return order;
} void traversal(Node* root, vector<int> &order) {
order.push_back(root->val);
int num = root->children.size();
for (int i = 0; i < num; i++) {
traversal(root->children.at(i), order);
}
}
};
2. Iterative solution:
/*
// 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> preorder(Node* root) {
vector<int> order = {};
stack<Node*> nodeStack;
if (root) {
nodeStack.push(root);
} while (!nodeStack.empty()) {
Node* node = nodeStack.top();
nodeStack.pop();
order.push_back(node->val);
int num = node->children.size();
for (int i = num - 1; i >= 0; i--) {
nodeStack.push(node->children.at(i));
}
} return order;
} };
【leetcode】589. N-ary Tree Preorder Traversal的更多相关文章
- 【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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】114. Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- matlab 只安装部分产品
- BZOJ 1801: [Ahoi2009]中国象棋
题目描述 //每行每列最多放两个,可以讨论第i-1行到第i行的每一种情况 #include<complex> #include<cstdio> using namespace ...
- js中for..of..的使用和迭代器
for..of是ES6中引入的新特性,它主要的作用是:循环一个可迭代的对象. 它可以循环遍历,数组.字符串.Set对象等等,先来看两个简单的例子: 遍历字符串 let str = 'Hello' fo ...
- Spring Cloud Ribbon源码分析---负载均衡实现
上一篇结合 Eureka 和 Ribbon 搭建了服务注册中心,利用Ribbon实现了可配置负载均衡的服务调用.这一篇我们来分析Ribbon实现负载均衡的过程. 从 @LoadBalanced入手 还 ...
- Cesium 1.54评测 【转】
重要功能评测 3dtiles数据上画线和贴纹理 3dtiles数据上画线和贴纹理 把线条贴到3dtiles上需要用到两个属性:clampToGround和classificationType. c ...
- 哈希表查找(散列表查找) c++实现HashMap
算法思想: 哈希表 什么是哈希表 在前面讨论的各种结构(线性表.树等)中,记录在结构中的相对位置是随机的,和记录的关键字之间不存在确定的关系,因此,在结构中查找记录时需进行一系列和关键字的比较.这一类 ...
- ERROR: type "sum" does not exist
开发问pg中执行一个简单的语句,多次报错: > ERROR: type "sum" does not exist LINE 1: SELECT SUM ^ 看看具体的语句,其 ...
- ipa重签名最直接的教程
ipa 包重签名最新最简单教程 重签名的意义:ipa 重签名最大的用处是,不必重新打包,和配置其它第三方获取 appkey 等操作,直接重签名之后依然可以拥有这些功能,更快的发布测试或者灰度版本. 本 ...
- deformable conv
在原feature map上经过卷积生成与原feature map一样w.h大小的feature map,但是channel变为2倍,即2N.2N代表的是每个像素x.y两个方向的偏移量. 这个偏移量生 ...
- QT5 编译提示 cannot find GL/gl.h ubuntu16.04
安装软件: sudo apt-get install mesa-common-dev sudo apt-get install libgl1-mesa-dev libglu1-mesa-dev