非递归遍历N-ary树Java实现
2019-03-25 14:10:51
非递归遍历二叉树的Java版本实现之前已经进行了总结,这次做的是非递归遍历多叉树的Java版本实现。
在非递归遍历二叉树的问题中我个人比较推荐的是使用双while的方式来进行求解,因为这种方法比较直观,和遍历的顺序完全对应。
但是在非递归遍历多叉树的时候,使用双while方法虽然依然可行,但是就没有那么方便了。
一、N-ary Tree Preorder Traversal
问题描述:
问题求解:
public List<Integer> preorder(Node root) {
if (root == null) return new ArrayList<>();
List<Integer> res = new ArrayList<>();
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
Node cur = stack.pop();
res.add(cur.val);
for (int i = cur.children.size() - 1; i >= 0; i--) if (cur.children.get(i) != null) stack.push(cur.children.get(i));
}
return res;
}
二、N-ary Tree Postorder Traversal
问题描述:
问题求解:
public List<Integer> postorder(Node root) {
if (root == null) return new ArrayList<>();
List<Integer> res = new ArrayList<>();
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
Node cur = stack.pop();
res.add(0, cur.val);
for (int i = 0; i < cur.children.size(); i++) if (cur.children.get(i) != null)
stack.push(cur.children.get(i));
}
return res;
}
三、N-ary Tree Level Order Traversal
问题描述:
问题求解:
public List<List<Integer>> levelOrder(Node root) {
if (root == null) return new ArrayList<>();
List<List<Integer>> res = new ArrayList<>();
Queue<Node> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
res.add(new ArrayList<>());
for (int i = 0; i < size; i++) {
Node cur = q.poll();
res.get(res.size() - 1).add(cur.val);
for (int j = 0; j < cur.children.size(); j++) {
if (cur.children.get(j) != null) q.offer(cur.children.get(j));
}
}
}
return res;
}
非递归遍历N-ary树Java实现的更多相关文章
- 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java
前一段时间,学习数据结构的各种算法,概念不难理解,只是被C++的指针给弄的犯糊涂,于是用java,web,javascript,分别去实现数据结构的各种算法. 二叉树的遍历,本分享只是以二叉树中的先序 ...
- 二叉树3种递归和非递归遍历(Java)
import java.util.Stack; //二叉树3种递归和非递归遍历(Java) public class Traverse { /******************一二进制树的定义*** ...
- 非递归遍历二叉树Java版的实现代码(没写层次遍历)
直接上代码呵呵,里面有注解 package www.com.leetcode.specificProblem; import java.util.ArrayList; import java.util ...
- JAVA递归、非递归遍历二叉树(转)
原文链接: JAVA递归.非递归遍历二叉树 import java.util.Stack; import java.util.HashMap; public class BinTree { priva ...
- Java实现二叉树的创建、递归/非递归遍历
近期复习数据结构中的二叉树的相关问题,在这里整理一下 这里包含: 1.二叉树的先序创建 2.二叉树的递归先序遍历 3.二叉树的非递归先序遍历 4.二叉树的递归中序遍历 5.二叉树的非递归中序遍历 6. ...
- 非递归遍历二叉树Java实现
2018-10-03 20:16:53 非递归遍历二叉树是使用堆栈来进行保存,个人推荐使用双while结构,完全按照遍历顺序来进行堆栈的操作,当然在前序和后序的遍历过程中还有其他的压栈流程. 一.Bi ...
- 数据结构之二叉树篇卷三 -- 二叉树非递归遍历(With Java)
Nonrecursive Traversal of Binary Tree First I wanna talk about why we should <code>Stack</c ...
- C++编程练习(17)----“二叉树非递归遍历的实现“
二叉树的非递归遍历 最近看书上说道要掌握二叉树遍历的6种编写方式,之前只用递归方式编写过,这次就用非递归方式编写试一试. C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历 ...
- c/c++二叉树的创建与遍历(非递归遍历左右中,破坏树结构)
二叉树的创建与遍历(非递归遍历左右中,破坏树结构) 创建 二叉树的递归3种遍历方式: 1,先中心,再左树,再右树 2,先左树,再中心,再右树 3,先左树,再右树,再中心 二叉树的非递归4种遍历方式: ...
随机推荐
- vim创建程序文件自动添加头部注释/自动文件头注释与模板定义
Vim 自动文件头注释与模板定义 在vim的配置文件.vimrc添加一些配置可以实现创建新文件时自动添加文件头注释,输入特定命令可以生成模板. 使用方法 插入模式输入模式输入seqlogic[Ente ...
- the pitfull way to create a uClinux image including gdb.
After downloaded and installed the GCT's SDK and toolchain, we try to make an our own image which in ...
- ng2-table
[转]https://github.com/valor-software/ng2-table demo:http://valor-software.com/ng2-table/ ng2-table ...
- git 不能创建分支
git 不能创建分支,如下 fatal: cannot lock ref 'refs/heads/hotfix/aa': 'refs/heads/hotfix' exists; cannot crea ...
- 原生js阻止表单跳转
/* W3C浏览器下的 */ var forms = document.getElementById("from") forms.addEventListener('submit' ...
- fastjson java类、字符串、jsonObject之前的转换
json对象转成json字符串 JSONObject json = new JSONObject(); json.put("page",1); json.put("pag ...
- redis内存不够 : OOM command not allowed when used memory > ‘maxmemory’
Redis内存不够,报错. 三种解决思路 注:如修改了配置文件需重启redis 1. 增加redis内存,修改redis.conf(集群中为redis-env.sh),默认为1024MB,增加到合适的 ...
- armv8 memory translation
AArch32,arm的32bit架构: AArch64,arm的64bit架构: ARMv8.2-LPA,是armv8.2中的新feature,扩大了IPA和PA的支持范围,从48bit扩展到52b ...
- 抓包工具 GOOGLE
chrome://net-internals/#events GOOGLE浏览器直接输入
- jw player 配置参数
Loading the player … //player所在div //具体配置参数 jwplayer(“container”).setup({//通过js调用播放器并安装到指定容器(contain ...