【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/n-ary-tree-preorder-traversal/description/
题目描述
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?
题目大意
N叉树的前序遍历。
解题方法
递归
首先得明白,这个N叉树是什么样的数据结构定义的。val是节点的值,children是一个列表,这个列表保存了其所有节点。
前序遍历,即首先把根节点的值放到list中,然后再遍历其子节点们的值,同时对于每一个子节点也做同样的操作。
时间复杂度是O(N),空间复杂度是O(1).
Python代码如下:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def preorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
res = []
if not root:
return res
res.append(root.val)
for child in root.children:
res.extend(self.preorder(child))
return res
C++代码如下:
/*
// 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> res;
if (!root) return res;
res.push_back(root->val);
for (Node* child : root->children) {
vector<int> child_vector = preorder(child);
res.insert(res.end(), child_vector.begin(), child_vector.end());
}
return res;
}
};
迭代
就像题目说的,递归方法很繁琐,能不能用迭代的方法去实现呢?答案很显然想到用栈!!
栈是一种后进先出的数据结构,先把root送入栈中,把它的数值放入结果中,然后需要把它的所有子节点逆序放入栈中。为什么是逆序?因为后进先出啊!我们需要在下一轮遍历它的最左孩子!
时间复杂度是O(N),空间复杂度是O(N).
Python代码如下:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def preorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
if not root: return []
stack = []
stack.append(root)
res = []
while stack:
node = stack.pop()
res.append(node.val)
stack.extend(node.children[::-1])
return res
C++代码如下:
/*
// 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> res;
if (!root) return res;
stack<Node*> s;
s.push(root);
while (!s.empty()) {
Node* node = s.top(); s.pop();
if (!node) continue;
res.push_back(node->val);
for (int i = node->children.size() - 1; i >= 0; --i) {
s.push(node->children[i]);
}
}
return res;
}
};
日期
2018 年 7 月 12 日 —— 天阴阴地潮潮,已经连着两天这样了
2018 年 11 月 5 日 —— 打了羽毛球,有点累
2019 年 9 月 20 日 —— 是选择中国互联网式加班?还是外企式养生?
【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)的更多相关文章
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- LeetCode 589 N-ary Tree Preorder Traversal 解题报告
题目要求 Given an n-ary tree, return the preorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返回 ...
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】590. N-ary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】1008. Construct Binary Search Tree from Preorder Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...
随机推荐
- Oracle-怎么在表的特定位置增加列
create table tmp as select ID,UserName,RealName,Sex,Tel,Addr from tabName;drop table tabName;rename ...
- Scrapy-Redis的安装和使用
Scrapy-Redis是Scrapy的分布式扩展模块,有了它,我们就可以方便地实现Scrapy分布式爬虫的搭建.GitHub:https://github.com/rmax/scrapy-redis ...
- 【风控算法】一、变量分箱、WOE和IV值计算
一.变量分箱 变量分箱常见于逻辑回归评分卡的制作中,在入模前,需要对原始变量值通过分箱映射成woe值.举例来说,如"年龄"这一变量,我们需要找到合适的切分点,将连续的年龄打散到不同 ...
- 初学js正则表达式之密码强度验证
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [Windows编程]模块遍历
模块遍历 整体思路 1.创建进程快照 2.遍历首次模块 3.继续下次遍历 4.模块信息结构体 相关API的调用 创建进程快照API HANDLE WINAPI CreateToolhelp32Snap ...
- Linux学习 - 环境变量配置文件
一.环境变量配置文件的作用 /etc/profile /etc/profile.d/*.sh ~/.bash_profile ~/.bashrc /etc/bashrc 1 /etc/profile的 ...
- SpringAOP浅析
1.问题 问题:想要添加日志记录.性能监控.安全监测 2.最初解决方案 2.1.最初解决方案 缺点:太多重复代码,且紧耦合 2.2.抽象类进行共性设计,子类进行个性设计,此处不讲解,缺点一荣俱荣,一损 ...
- SpringIOC原理浅析
1. IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机械 ...
- mybatis的dao层和service层的编码设计的配置
/** 书写pojo类------>dao接口------>resources下建立同路径的dao.xml------>配置applicationContext.xml文件 **/ ...
- gitlab的分支保护配置
目录 一.简介 二.Gitlab配置步骤 一.简介 开发当前开发的分支遇到暂时无法解决的问题,现在有需要开发其他应用,所以希望运维这边将当前有问题分支冻结,让其他人无法进行修改,待后续有时间在排查代码 ...