/**
* Definition for binary tree
* 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> res = new ArrayList<Integer>();
if(null == root)
return res;
Stack<TreeNode> s = new Stack<TreeNode>();
while(!s.isEmpty()||null != root){
while(null != root){
s.push(root);
root = root.left;
}
root = s.pop();
res.add(root.val);
root = root.right;
}
return res;
}
}
原理同前序遍历。