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. python学习笔记(三)——函数

    函数定义 def 函数名(形参 . . . ) 函数体 1. 函数参数 返回值:可以有一个或多个 形参:支持默认形参.关键字形参.可变参数形参等 1.1 必须参数 调用时传入的参数必须与定义时相同. ...

  2. 《剑指offer》面试题2:实现Singleton 模式

    面试题2:实现Singleton 模式 题目:设计一个类,我们只能生成该类的一个实例.   只能生成一个实例的类是实现了Singleton (单例)模式的类型.由于设计模式在面向对象程序设计中起着举足 ...

  3. 顺利通过EMC实验(20)

  4. transformjs 污染了 DOM?是你不了解它的强大

    原文链接:https://github.com/AlloyTeam/AlloyTouch/wiki/Powerful-transformjs 写在前面 上星期在React微信群里,有小伙伴觉得tran ...

  5. JS+CSS实现数字滚动

    最近在实现一个显示RGB颜色数值的动画效果时,尝试使用了writing-mode(书写模式)及 text-orientation来实现文字的竖直方向的排列,并借助CSS的transition(过渡)来 ...

  6. 记一次用mpvue框架搭建的小程序

    介绍 mpvue (github 地址请参见)是一个使用 Vue.js 开发小程序的前端框架.框架基于 Vue.js 核心,mpvue 修改了Vue.js 的 runtime 和 compiler 实 ...

  7. sparksql Seq生成DataFrame

    首先,使用样例类: case class User(id:Int,name: String,gender:String, age: Int) 之后使用Seq创建Dataframe val alice: ...

  8. win内核漏洞提权

    WIN系统溢出漏洞提权 漏洞筛选 在整个提权项目中,前提是拿到webshell. 关于系统的溢出漏洞,我推荐两个项目: https://github.com/chroblert/WindowsVuln ...

  9. 设置IDEA启动时不打开上次项目

    步骤 1.启动IDEA,点击File 2.点击setting,在Appearance&Behavior中找到System Setting 3.取消勾选Reopen projects on st ...

  10. 从数据库中获取图片编号,然后通过request获取图片下载

    import pandas as pd from pandas.core.dtypes.dtypes import register_extension_dtype from sqlalchemy i ...