一、递归实现
1、前序遍历
前序遍历的顺序是:中左右
public class preOrderSolution {
public void preOrder(TreeNode root){
ArrayList<Integer> res = new ArrayList<>();
if(root == null)
return res;
res.add(root.val);
preOrder(root.left);
preOrder(root.right);
}
}
2、中序遍历
中序遍历的顺序是:左中右
public class inOrderSolution {
public void inOrder(TreeNode root){
ArrayList<Integer> res = new ArrayList<>();
if(root == null)
return res;
inOrder(root.left);
res.add(root.val);
inOrder(root.right);
}
}
3、后序遍历
后序遍历的顺序是:左右中
public class postOrderSolution {
public void postOrder(TreeNode root){
ArrayList<Integer> res = new ArrayList<>();
if(root == null)
return res;