二叉树的中序遍历
来源:互联网 发布:姚明球衣退役 知乎 编辑:程序博客网 时间:2024/06/15 19:31
class Solution {//递归实现ArrayList<Integer> list = new ArrayList<Integer>(); public ArrayList<Integer> midorderTraversal(TreeNode root) { if(root != null){ midorderTraversal(root.left); list.add(root.val); midorderTraversal(root.right); } return list; } //循环实现 public ArrayList<Integer> midorderTraversal2(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode cur = root, visit = null; while(cur != null || !stack.isEmpty()){ //循环遍历左孩子 if(cur != null){ stack.push(cur); cur = cur.left; }else{ cur = stack.peek(); if(cur.right == null || cur.right == visit){ visit = stack.pop();; cur = null; }else{ list.add(cur.val); cur = cur.right; } } } return list; }}
阅读全文
0 0
- 二叉树的中序遍历
- 二叉树的中序遍历
- 线索二叉树的中序遍历
- 有关二叉树的中序遍历
- 二叉树的中序遍历
- 排序二叉树的中序遍历
- 二叉树的中序遍历算法
- 题目:二叉树的中序遍历
- 二叉树的中序遍历
- 二叉树的中序遍历
- 二叉树的中序遍历
- LintCode -- 二叉树的中序遍历
- lintcode 二叉树的中序遍历
- 二叉树的前后中序遍历
- 二叉树的中序遍历
- 二叉树的中序遍历
- lintcode:二叉树的中序遍历
- lintcode,二叉树的中序遍历
- 内存的堆分配和栈分配
- ASP.NET-Application对象
- 关于jsp的静态和动态引入文件问题
- PHP 实现四种基本排序算法
- JS(去掉前后空格或去掉所有空格)的用法
- 二叉树的中序遍历
- Spring MVC + thymeleaf 非配置
- 二叉树递归问题 leetcode 572. Subtree of Another Tree & leetcode 110. Balanced Binary Tree
- java使用poi来写入excel
- Ubiquitous Religions POJ
- sql参数绑定
- DTW(Dynamic Time Warping / 动态时间归整) python实现
- python编辑excel做报表给manager看
- Angularjs 获取checkbox所有选中的值