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 ...
随机推荐
- ROS机器人操作系统相关书籍、资料和学习路径
作者:Top Liu链接:https://zhuanlan.zhihu.com/p/30391098来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本文是易科机器人实验 ...
- 腾讯云+社区开发者大会开启报名,WeGeek 邀你一起聊聊小程序
刚满 2 岁的微信小程序,正给我们带来一种全新轻便的生活方式. 内测时的青涩还历历在目,到现在,小程序生态已日渐成熟.超过 150 万开发者在这里找到了自己的新天地,打磨出超过 100 万个小程序. ...
- 微信小程序自定义tab,多层tab嵌套实现
小程序最近是越来越火了-- 做小程序有一段时间了,总结一下项目中遇到的问题及解决办法吧. 项目中有个多 tab 嵌套的需求,进入程序主界面下面有两个 tab,进入A模块后,A模块最底下又有多个tab, ...
- Win7运行net5 wpf条件
Win7运行net5 wpf条件 win7 sp1 dotnet-runtime-5 vc_redist KB2999226 KB4457144 Tips:官网条件最后一个最坑爹,KB2533623不 ...
- BlockingCollection实现生产者消费者
很简单的代码,看注释 class Program { private static ManualResetEvent _manualResetEvent = new ManualResetEvent( ...
- c++对c的拓展_增强
一:新增bool类型关键字:c中bool类型需要添加stdbool.h头文件,c++则可直接使用 void test(){ bool a=true; //c++可直接定义而c需添加头文件 true和f ...
- Java核心知识1:泛型机制详解
1 理解泛型的本质 JDK 1.5开始引入Java泛型(generics)这个特性,该特性提供了编译时类型安全检测机制,允许程序员在编译时检测到非法的类型. 泛型的本质是参数化类型,即给类型指定一个参 ...
- Infrastructure 知识: DNS 命令: dig, host
dig 基本用法: dig @server name type 或者用-t type来指定(更常见) dig @server -t type name 例子详解 # 最简单的使用 $ dig www. ...
- Python学习笔记: 装饰器Decorator
介绍 装饰器是对功能函数的加强. 在原来的功能函数之外,另外定义一个装饰器函数,对原来的功能函数进行封装(wrapper)并在wrapper的过程中增加一些辅助功能. 应用场景 如下场景: 业务函数f ...
- opencv如何在jupyter notebook中显示图片
方法一: from matplotlib import pyplot as plt import numpy as np import cv2 img = cv2.imread('img.jpg') ...