class Solution {
private List<Integer> retlist = new LinkedList<>();
public List<Integer> inorderTraversal(TreeNode root) {
//中序遍历,左臂入栈
TreeNode cur = root;
Stack<TreeNode> stack = new Stack<>();
while(cur!=null || !stack.isEmpty()){
if(cur!=null){
stack.add(cur);
cur = cur.left;
}
else{
cur = stack.pop();
retlist.add(cur.val);
cur = cur.right;
}
}
return retlist;
}
}
Iterative writing, the core idea is the past, and only a stack as a basis to judge, this time there are two judgments based on TreeNode cur and stack.
If cur is not empty, added Stack, the pointer to the left, if it is empty a pop, the return value is added, then the pointer pointing to the right