博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉树中序遍历
阅读量:4665 次
发布时间:2019-06-09

本文共 962 字,大约阅读时间需要 3 分钟。

二叉树中序遍历,分为递归和非递归方法:

递归:

private static List
inList = new ArrayList<>();public static List
inorderTraversalRec(TreeNode root){ if (null == root){ return inList; } inorder(root); return inList;}private static void inorder(TreeNode root){ if (null == root){ return; } inorder(root.left); inList.add(root.val); inorder(root.right);}

非递归:

public static List
inorderTraversal(TreeNode root) { List
list = new ArrayList<>(); if (null == root){ return list; } Stack
stack = new Stack<>(); TreeNode cur = root; while (null != cur || !stack.isEmpty()){ while (null != cur){ stack.push(cur); cur = cur.left; } TreeNode node = stack.pop(); list.add(node.val); cur = node.right; } return list;}

转载于:https://www.cnblogs.com/earthhouge/p/11262916.html

你可能感兴趣的文章
Nginx 独立图片服务器的搭建
查看>>
【原】js实现复制到剪贴板功能,兼容所有浏览器
查看>>
通过Nginx+tomcat+redis实现反向代理 、负载均衡及session同步
查看>>
iOS数据持久化-OC
查看>>
BeanUtils包的学习
查看>>
14.前端路由router-04编程式导航
查看>>
Awstats显示国家地区插件GeoIP安装
查看>>
Binary Tree Maximum Path Sum
查看>>
line,tiggke,fsm,condition,branch,assert coverage
查看>>
数字签名是什么?
查看>>
实现动态加载一个 JavaScript 资源
查看>>
iOS中push视图的时候,屏幕中间会出现一条灰色的粗线的解决方案
查看>>
[SCSS] Reuse Styles with the SCSS @mixin Directive
查看>>
4. Add override methods to class
查看>>
直播视频插件--sewise player
查看>>
ltp执行过程总结
查看>>
10套免费的响应式布局 Bootstrap 模版
查看>>
Tomcat 性能优化之APR插件安装 -- [转]
查看>>
字符串操作、文件操作,英文词频统计预处理
查看>>
web学习篇之http协议
查看>>