public class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res=new ArrayList<Integer>(); Stack<TreeNode> stack=new Stack<TreeNode>(); while(root!=null||!stack.isEmpty()) { while(root!=null) { stack.push(root); root=root.left; } root=stack.pop(); res.add(root.val); root=root.right; } return res; } }
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<Integer>();
TreeNode cur=root, pre=null;
while(cur!=null)
{
if(cur.left!=null)
{
pre=cur.left;
while(pre.right!=null&&pre.right!=cur)
pre=pre.right;
if(pre.right==null)
{
pre.right=cur;
cur=cur.left;
}
else
{
res.add(cur.val);
pre.right=null;
cur=cur.right;
}
}
else
{
res.add(cur.val);
cur=cur.right;
}
}
return res;
}
}