classSolution{public List<Integer>inorderTraversal(TreeNode root){
List<Integer> list =newArrayList<>();
Stack<TreeNode> stack =newStack<>();
TreeNode current = root;while(!stack.isEmpty()|| current != null){while(current != null){
stack.push(current);
current = current.left;}if(!stack.isEmpty()){
current = stack.pop();
list.add(current.val);
current = current.right;}}return list;}}
后序
classSolution{public List<Integer>postorderTraversal(TreeNode root){
List<Integer> list =newArrayList<>();
Stack<TreeNode> stack =newStack<TreeNode>();
TreeNode current = root;
TreeNode lastVisit = root;while(current != null){
stack.push(current);
current = current.left;}while(!stack.isEmpty()){
current = stack.pop();if(current.right != null && current.right != lastVisit){
stack.push(current);
current = current.right;while(current != null){
stack.push(current);
current = current.left;}}else{
list.add(current.val);
lastVisit = current;}}return list;}}
二叉树的前序、中序、后序遍历(递归与非递归)递归前序static class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); preTraverse(root, list); return list;