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. c、c++中-int型以float或者float型以int输出问题

    1.将浮点型以整形的类型输出问题 用VC6.0,会把以整形输出形式的浮点数输出为0: 1 #include"stdio.h" 2 int main() 3 { 4 float x= ...

  2. HTML+CSS基础课程-imooc-【更新完毕】

    6-1 认识CSS样式 CSS全称为"层叠样式表 (Cascading Style Sheets)",它主要是用于定义HTML内容在浏览器内的显示样式,如文字大小.颜色.字体加粗等 ...

  3. python爬取京东评论

    一.分析 1.找到京东商品评论所在位置(记得点击商品评论,否则找不到productPageComments.action)  2.解析文件 打开后发现是json数据,但不是那么规范,所以需要去点前面的 ...

  4. 关于vue中v-for的键值顺序

    在学习vue2.0时,关于处理v-for键值顺序时发现的问题: <body> <!-- 普通循环 --> <!-- {{num}} --> <!-- 列表循环 ...

  5. EMS创建独立新用户并分配邮箱

    创建新用户"王春海"并分配邮箱. 以Exchange管理员身份登录EMS控制台.在PowerShell命令行提示符下,键入如下命令: [PS] C:\Windows\system3 ...

  6. 走进JUC的世界

    概念 同步锁:synchronized.Lock区别 1.synchronized是不需要进行手动解锁 2.synchronized可以锁方法.锁同步代码块 3.synchronized是Java自带 ...

  7. python入门-开始

    1.为啥要学Python? 各种语言的优劣势对比视频版:https://www.bilibili.com/video/BV1y3411r7pX/?spm_id_from=autoNext 各种语言的优 ...

  8. redis5.0.0集群搭建【实战经历】

    redis集群搭建 作者:陈土锋 时间:2020年6月2日 目录 一.环境介绍... 1 1.机器准备... 1 2.关闭防护墙和selinux. 1 3.时间同步... 1 二.Redis Clus ...

  9. Java三大结构

    Java三大结构 顺序结构(基本结构) 选择结构 循环结构 1. 顺序结构 平时一般语句都默认遵循顺序结构 2. 选择结构 2.1 if单选择结构 语法 if(布尔表达式){ //布尔表达式为true ...

  10. TCP/IP 协议标准简单描述

    TCP/IP 协议标准简单描述 说明 分为三部分:中文名称.缩写.说明. 应用层 DNS 域名服务 (DNS) 功能: 将域名转化为IP地址 BOOTP 引导程序协议 (BOOTP) 功能: 允许无盘 ...