题目地址: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 日 —— 打了羽毛球,有点累

最新文章

  1. js self = this的解释
  2. POJ3070 Fibonacci[矩阵乘法]
  3. jquery数组(排序)
  4. UIProgressView改变高度
  5. python webdriver 自动化测试练习 1-- 在线调查
  6. Lua标准库- 模块(Modules)
  7. Facebook Hacker Cup 2015 Round 1--Corporate Gifting(树动态规划)
  8. tomcat的自我理解与使用心得
  9. textarea内容太多, 鼠标点击全部显示
  10. hdu 4747 线段树
  11. NeuChar 平台使用及开发教程(四):使用 NeuChar 的素材服务
  12. js之Ajax下载文件
  13. 统计分析与R软件-chapter2-2
  14. windows 安装mysql 5.7的正确姿势
  15. 在Visualforce页面中使用Visual Flow
  16. 源码分析之CountDownLatch
  17. JDBC辅助类封装 及应用
  18. MVVM 简化的Messager类
  19. PHP会话控制之如何正确设置session_name
  20. [IOS]VMware上虚拟机MAC安装XCode

热门文章

  1. 搭建简单的SpringCloud项目三:问题及解决
  2. Visual Studio Code常用操作整理
  3. day02 MySQL基本操作
  4. mybatis-plus解析
  5. Java线程安全性-原子性工具对比
  6. java使用在线api实例
  7. java输入/输出流的基本知识
  8. 30个类手写Spring核心原理之Ioc顶层架构设计(2)
  9. mysql中索引,触发器,事务,存储引擎的理解
  10. Jsp/servlet分页五要素