LintCode Binary Tree Inorder Traversal 二叉树的中序遍历(非递归)
2015-07-14 14:03
435 查看
给出一棵二叉树,返回其中序遍历。
Given a binary tree, return the inorder traversal of its nodes’ values.
样例
给出二叉树 {1,#,2,3},
1
\
2
/
3
返回 [1,3,2].
Given a binary tree, return the inorder traversal of its nodes’ values.
样例
给出二叉树 {1,#,2,3},
1
\
2
/
3
返回 [1,3,2].
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The root of binary tree. * @return: Inorder in ArrayList which contains node values. */ public ArrayList<Integer> inorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode p = root; while(p != null || !stack.empty()) { while(p != null) { stack.push(p); p = p.left; } if(!stack.empty()) { p = stack.pop(); list.add(p.val); p = p.right; } } return list; } }
相关文章推荐
- python 的日志logging模块学习
- 标准二维表
- 【IE6的BUG一】链接伪类(:hover)CSS背景图片有闪动BUG
- 设计模式
- 文件换行符
- mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录
- 【最短路】poj3660 最短路的应用
- linux使用fdisk添加分区的例子
- leetcode[190]:Reverse Bits
- 【Java】给定有向图,设计一个算法,找出两个结点之间是否存在一条路径
- 【转载】JConsole JDK1.6 使用手册
- (L1) AudioService AudioHandler setDeviceVolume, setAllVolumes, persistVolume
- IOS中判断设备型号的宏定义
- JS取出字符串的12种方法
- centos6.2安装jdk7
- HLS 测试用流地址
- flash 时钟
- 服务器安全配置参考
- 遍历Map集合的集中方法(最后一句话亮亮的)
- poj 3784 用堆动态求解中位数