LeetCode 94. Binary Tree Inorder Traversal 解题报告
2016-02-18 10:59
441 查看
94. Binary Tree Inorder Traversal
My SubmissionsQuestion
Total Accepted: 109988 Total
Submissions: 284121 Difficulty: Medium
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree
{1,#,2,3},
1 \ 2 / 3
return
[1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?
confused what
"{1,#,2,3}"means? >
read more on how binary tree is serialized on OJ.
Subscribe to see which companies asked this question
Show Tags
Show Similar Problems
Have you met this question in a real interview?
Yes
No
Discuss
二叉树的中序遍历。递归解法自不必说,题目已经说明了,递归求解十分 trivial。要求用非递归方式求解!
给出AC的递归和非递归代码
public class BinaryTreeInorderTraversal { public static void main(String[] args) { TreeNode n1 = new TreeNode(2); TreeNode n2 = new TreeNode(1); n1.left = n2; System.out.println(inorderTraversal(n1)); } public static List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<Integer>(); in(root, list); return list; } private void inRecursive(TreeNode root, List<Integer> l) { if (root == null) return; if (root.left != null) in(root.left, l); l.add(root.val); if (root.right != null) in(root.right, l); } private static void in(TreeNode root, List<Integer> l) { if (root == null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode cur = root; while (!stack.isEmpty() || cur != null) { if (cur != null) { stack.push(cur); cur = cur.left; } else { cur = stack.pop(); l.add(cur.val); cur = cur.right; } } } }
相关文章推荐
- python模块介绍- collections(5)-OrderedDict 有序字典
- C语言中的static关键字
- kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)
- Linux 实现多条ADSL负载均衡
- sqlsever数据库springmvc mybatis bootstrap html5
- 面向接口编程,接口的优点
- JAVA List 并集, 交集, 差集
- 常用iOS第三方库以及XCode插件介绍
- ACM 算法竞赛 八大基础算法 模拟法 字符串 递归分治 排序 枚举 贪心 递推
- iTween基础之Scale(缩放大小)
- json转换为各种数据对象格式
- visual 2008启动exe时报错:プロシージャ エントリ ポイント ?_Xweak@tr1@std@@YAXXZ がダイナミック リンク ライブラリ MSVCP90D.dll から見つかりません
- Python filter,map,lambda,reduce,列表解析
- Php数据类型简介
- iTween基础之Scale(缩放大小)
- [Java] Java API文档下载方法
- 时间比较
- linux octave 4.0安装
- 一枚招聘信息——微信支付web前端开发工程师【已招到】
- PHP——分页显示数据库内容