中序遍历就是:左根右
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
//栈
Deque<TreeNode> q = new LinkedList<>();
List<Integer> ans = new LinkedList<>();
while(root != null || q.size() > 0) {
//一直把左孩子入栈
while(root != null) {
q.push(root);
root = root.left;
}
TreeNode node = q.pop();
ans.add(node.val);
root = node.right;
}
return ans;
}
}