590. N-ary Tree Postorder Traversal - LeetCode
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的更多相关文章
- Binary Tree Postorder Traversal leetcode java
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- Binary Tree Postorder Traversal --leetcode
原题链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题目大意:后序遍历二叉树 解题思路:后序遍历二叉树的步骤: ...
- LeetCode 590. N叉树的后序遍历(N-ary Tree Postorder Traversal)
590. N叉树的后序遍历 590. N-ary Tree Postorder Traversal 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. LeetCode590. N-ary Tre ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- [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 ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- 【leetcode_easy】590. N-ary Tree Postorder Traversal
problem 590. N-ary Tree Postorder Traversal 参考 1. Leetcode_easy_590. N-ary Tree Postorder Traversal; ...
- LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
随机推荐
- ubuntu sublime text3 python 配置 sublime text3 python 配置
ubuntu sublime text3 python 配置 1.安装sublime text 3 安装过程非常简单,在terminal中输入: sudo add-apt-repository ...
- 联想电脑“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态” 解决方法
当在虚拟机上安装Ubuntu系统时,出现 "此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态" 弹窗此时需要进入BIOS修改相关的设置,此处以联想ideap ...
- Web前端初级问题—ajax登录跳转登录实现
当我们的用户进行系统登录时,用户名和密码的验证都是后端验证的.而且,用户登录状态也是要后端设置的,查询数据库后,用户名和密码正确,则在session中存储一个uuid,每个页面需要根据登录状态判断展示 ...
- 腾讯云+社区开发者大会开启报名,WeGeek 邀你一起聊聊小程序
刚满 2 岁的微信小程序,正给我们带来一种全新轻便的生活方式. 内测时的青涩还历历在目,到现在,小程序生态已日渐成熟.超过 150 万开发者在这里找到了自己的新天地,打磨出超过 100 万个小程序. ...
- 前端调试利器 - Charles
Docs 开发之 Charles 配置指南 1.下载与安装 charles-proxy-4.1.4 .dmg56.12 MB已存到云盘下载 2.激活 使用公司正版license 激活 安装证书 点击证 ...
- sticker-footer 布局
sticker-footer 1.嵌套层级不深,可直接继承自 body width:100%: height:100%; // html <body> <div id="s ...
- mysql的下载和安装详细教程(windows)
Windows下安装MySQL详细教程 1.安装包下载 2.安装教程 (1)配置环境变量 (2)生成data文件 (3)安装MySQL (4)启动服务 (5)登录MySQL (6)查询用户密码 ...
- mixin和composition api
1. 这两个都是实现组件逻辑复用的法宝 2. composition api是vue3的, composition api的出现就是解决mixins的不足之处的 一. mixin 回顾下mixin, ...
- 正则、字符类Pattern、Matcher类
字符类 * [abc] a.b 或 c(简单类) * [^abc] 任何字符,除了 a.b 或 c(否定) * [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) * [0-9 ...
- [ SOS ] 版本控制工具 笔记
https://www.cnblogs.com/yeungchie/ soscmd 创建工作区 soscmd newworkarea $serverName $projectName [$path] ...