题目地址:https://leetcode.com/problems/n-ary-tree-postorder-traversal/description/

题目描述

Given an n-ary tree, return the postorder traversal of its nodes’ values.

For example, given a 3-ary tree:

Return its postorder traversal as: [5,6,3,2,4,1].

Note: Recursive solution is trivial, could you do it iteratively?

题目大意

N叉树的后序遍历。

解题方法

递归

首先得明白,这个N叉树是什么样的数据结构定义的。val是节点的值,children是一个列表,这个列表保存了其所有节点。

后序遍历,如果通过递归还是非常简单的。对其子节点遍历,在对其本身节点遍历即可。由于所有的子节点是个列表,这样甚至比二叉树还要简单,只需对列表进行循环就行了。

Python代码如下:

"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def postorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
res = []
if not root:
return res
for child in root.children:
res.extend(self.postorder(child))
res.append(root.val)
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> postorder(Node* root) {
vector<int> res;
if (!root) return res;
for (Node* child : root->children) {
vector<int> child_vector = postorder(child);
res.insert(res.end(), child_vector.begin(), child_vector.end());
}
res.push_back(root->val);
return res;
}
};

迭代

这个题希望我们使用迭代方法去做,即使用迭代的方法得到后序遍历。由于后序遍历把根节点放到了最后,而我们在遍历的过程中,一定先获得到根节点,那么我们可以先倒序,然后再反转。

后序遍历:左->右->根
我们的做法:根->右->左,然后再反转。

即,先把根节点放入栈中,然后把它的孩子从左到右依次放入,这样我们下次对栈内的元素遍历得到的顺序就是从右向左的,对于栈中弹出的每个节点都是如此。

得到的顺序是根->右子树(节点全部入栈)->左子树的遍历方式,最后需要加一个翻转即可得到想要的后序遍历。

Python代码如下:

"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def postorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
res = []
if not root:
return res
stack = [root,]
while stack:
node = stack.pop()
stack.extend(node.children)
res.append(node.val)
return res[::-1]

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> postorder(Node* root) {
vector<int> res;
if (!root) return res;
stack<Node*> st;
st.push(root);
while (!st.empty()) {
Node* node = st.top(); st.pop();
if (!node) continue;
for (Node* child : node->children) {
st.push(child);
}
res.push_back(node->val);
}
reverse(res.begin(), res.end());
return res;
}
};

相似题目

https://blog.csdn.net/fuxuemingzhu/article/details/81021950

参考资料

https://leetcode.com/articles/n-ary-tree-postorder-transversal/

日期

2018 年 7 月 12 日 —— 天阴阴地潮潮,已经连着两天这样了
2018 年 11 月 5 日 —— 打了羽毛球,有点累

【LeetCode】590. N-ary Tree Postorder Traversal 解题报告 (C++&Python)的更多相关文章

  1. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  2. 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  3. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  4. LeetCode 590 N-ary Tree Postorder Traversal 解题报告

    题目要求 Given an n-ary tree, return the postorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返 ...

  5. 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  8. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  9. 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)

    [LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...

随机推荐

  1. 再聊我们自研的那些Devops工具

    两年前我写了篇文章『我们自研的那些Devops工具』介绍了我们自研的一些DevOps工具系统,两年过去了这些工具究竟还有没有在发光发热,又有哪些新的变化呢,我将通过这篇文章来回顾一下这两年的发展与变化 ...

  2. admire, admit

    admire 当别人admire你时,小心掉进泥潭(mire).词源:to wonder. wonderful夸人,awful骂人,awesome夸人.admiral与admire词源不同,碰巧长得像 ...

  3. acupuncture

    acute+puncture. [woninstitute.edu稻糠亩] To understand the basics of acupuncture, it is best to familia ...

  4. acquire

    An acquired taste is an appreciation for something unlikely to be enjoyed by a person who has not ha ...

  5. Linux基础命令---ab测试apache性能

    ab ab指令是apache的性能测试工具,它可以测试当前apache服务器的运行性能,显示每秒中可以处理多少个http请求. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.F ...

  6. 【Java 8】Stream.distinct() 列表去重示例

    在这篇文章里,我们将提供Java8 Stream distinct()示例. distinct()返回由该流的不同元素组成的流.distinct()是Stream接口的方法. distinct()使用 ...

  7. 使用AOP思想实现日志的添加

    //1.创建日志表syslog------->创建日志的实体类--------->在web.xml中配置监听 <listener>     <listener-class ...

  8. 测试工具_http_load

    目录 一.简介 二.例子 三.参数 一.简介 http_load以并行复用的方式运行,用以测试Web服务器的吞吐量与负载.但是它不同于大多数压力测试工具,其可以以一个单一的进程运行,这样就不会把客户机 ...

  9. 联盛德 HLK-W806 (十一): 软件SPI和硬件SPI驱动ST7567液晶LCD

    目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...

  10. .NET Core工程应用系列(2) 实现可配置Attribute的Json序列化方案

    背景 在这篇文章中,我们实现了基于自定义Attribute的审计日志数据对象属性过滤,但是在实际项目的应用中遇到了一点麻烦.需要进行审计的对象属性中会包含其他类对象,而我们之前的实现是没办法处理这种类 ...