Question

590. N-ary Tree Postorder Traversal

Solution

题目大意:后序遍历一个树

思路:

1)递归

2)迭代

Java实现(递归):

public List<Integer> postorder(Node root) {
List<Integer> ansList = new ArrayList<>();
recursivePostorder(root, ansList);
return ansList;
} void recursivePostorder(Node root, List<Integer> ansList) {
if (root == null) return;
if (root.children != null) {
for (Node tmp : root.children) {
recursivePostorder(tmp, ansList);
}
}
ansList.add(root.val);
}

Java实现(迭代):

public List<Integer> postorder(Node root) {
List<Integer> ansList = new ArrayList<>();
if (root == null) return ansList;
List<Node> nodeList = new ArrayList<>();
nodeList.add(root);
while (nodeList.size() > 0) {
Node cur = nodeList.get(nodeList.size() - 1);
nodeList.remove(nodeList.size() - 1);
ansList.add(cur.val);
if (cur.children != null) {
for (Node tmp : cur.children) {
nodeList.add(tmp);
}
}
}
for (int i=0; i<ansList.size()/2; i++) {
int tmp = ansList.get(i);
int end = ansList.size() - 1 - i;
ansList.set(i, ansList.get(end));
ansList.set(end, tmp); }
return ansList;
}

590. N-ary Tree Postorder Traversal - LeetCode的更多相关文章

  1. Binary Tree Postorder Traversal leetcode java

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...

  2. Binary Tree Postorder Traversal --leetcode

    原题链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题目大意:后序遍历二叉树 解题思路:后序遍历二叉树的步骤: ...

  3. LeetCode 590. N叉树的后序遍历(N-ary Tree Postorder Traversal)

    590. N叉树的后序遍历 590. N-ary Tree Postorder Traversal 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. LeetCode590. N-ary Tre ...

  4. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  5. [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 ...

  6. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  7. 【leetcode_easy】590. N-ary Tree Postorder Traversal

    problem 590. N-ary Tree Postorder Traversal 参考 1. Leetcode_easy_590. N-ary Tree Postorder Traversal; ...

  8. LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard

    题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...

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

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

随机推荐

  1. MATLAB设计模糊控制器并用simulink仿真

    一.设计模糊控制器1.1 创建项目文件夹在此路径如图 1.2 打开MATLAB打开MATLAB R2012a切换当前目录为上一步路径,如图 1.3 设计模糊控制器打开模糊控制器设计对话框 根据模糊控制 ...

  2. 顺利通过EMC实验(6)

  3. template7入门教程及对它的一些看法

    template7是framework7的内置模板引擎,在此之前使用过jquery-tmpl,不过刚刚打开github看了下,已经停止更新,并且将要被JsRender所替代.妹的,JsRender又是 ...

  4. 深入HTTP协议

    一.HTTP定义 超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. HTTP是一个属于应用层的面向对象协议,由于其简捷.快速的方式, ...

  5. Shiro 安全框架详解二(概念+权限案例实现)

    Shiro 安全框架详解二 总结内容 一.登录认证 二.Shiro 授权 1. 概念 2. 授权流程图 三.基于 ini 的授权认证案例实现 1. 实现原理图 2. 实现代码 2.1 添加 maven ...

  6. Exchange 2013 清空邮箱

    在某些应用场景中,需要清空用户邮箱的所有数据.如果使用Outlook web app或者Outlook 的邮件删除方式,对数以千计的邮件来说,实在不是一个好办法.exchange管理员可以使用&quo ...

  7. 输入n,然后输入n个数,使它升序输出

    #include<iostream> using namespace std; int main() { int n,i,j,m,k; cin>>n; int a[n];  f ...

  8. js知识梳理6:关于函数的要点梳理(2)(作用域链和闭包)

    写在前面 注:这个系列是本人对js知识的一些梳理,其中不少内容来自书籍:Javascript高级程序设计第三版和JavaScript权威指南第六版,感谢它们的作者和译者.有发现什么问题的,欢迎留言指出 ...

  9. python---用顺序表实现双端队列

    class Dqueue(object): """双端队列""" def __init__(self): self.__list = [] ...

  10. numpy---(上)

    Numpy Numpy ndarray N维数组对象ndarray, 是一系列同类型数据的集合, 索引以0下标开始, 创建一个ndarray对象, 需调用array函数: numpy.array(ob ...