标签:
/**
* 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;
}
}
标签:
原文地址:http://www.cnblogs.com/neweracoding/p/4837800.html