码迷,mamicode.com
首页 > 其他好文 > 详细

Binary Tree Inorder Traversal

时间:2015-09-25 13:19:53      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ints = new ArrayList<Integer>();

if(root == null)
return ints;

ArrayDeque<TreeNode> nodes = new ArrayDeque<TreeNode>();

TreeNode n = root;
while(n!=null || !nodes.isEmpty())
{
if(n == null)
{
TreeNode consume = nodes.pop();
ints.add(consume.val);
n = consume.right;
}
else
{
if(n.left == null)
{
ints.add(n.val);
n = n.right;
}
else
{
nodes.push(n);
n = n.left;
}
}

}

return ints;
}
}

Binary Tree Inorder Traversal

标签:

原文地址:http://www.cnblogs.com/neweracoding/p/4837800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!