题目描述
AC代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<>();
Stack<TreeNode> stk=new Stack<>();
TreeNode p=root;
while(p!=null||!stk.empty()){
while(p!=null){
stk.push(p);
p=p.left;
}
p=stk.peek();
stk.pop();
res.add(p.val);
p=p.right;
}
return res;
}
}