144.二叉树的前序遍历
        给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
示例 1:

输入:root = [1,null,2,3]
输出:[1,2,3]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
示例 4:

输入:root = [1,2]
输出:[1,2]
问题分析:
①递归法:
1、确定递归函数的参数和返回值
2、确定终止条件
3、确定单层递归的逻辑
②迭代法:
把节点先压入栈中,再从栈中弹出放入数组。 注意压入栈中顺序:前右左,这样才能在弹出元素时变成前左右
方法一:递归法
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        preOrder(root,result);
        return result;
    }
    public void preOrder(TreeNode root,List<Integer> result){//确定递归函数的参数和返回值
        if (root==null){//确定终止条件
            return;
        }
        //确定单层递归的逻辑
        result.add(root.val);
        preOrder(root.left,result);
        preOrder(root.right,result);
    }
}方法二:迭代法
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();//数组
        Stack<TreeNode> st=new Stack<>();//栈
       if (root==null){
           return result;
        }
       st.push(root);
       while(!st.isEmpty()){
           TreeNode node=st.pop();
           result.add(node.val);
           if(node.right!=null){
               st.push(node.right);
           }
           if(node.left!=null){
               st.push(node.left);
           }
       }
       return result;
    }
}145.二叉树的后序遍历
        给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。
示例 1:

输入:root = [1,null,2,3]
输出:[3,2,1]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
问题分析:
①递归法
②迭代法:如果指针不为空,先处理左节点,让其弹入到栈中,如果有左孩子就继续弹入,指针左移。 如果指针为空,指针返回栈顶节点,并弹出栈顶节点,指针再移到此节点的右孩子,再继续判断是否为空。
方法一:递归法
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        postOrder(root,result);
        return result;
    }
    public void postOrder(TreeNode root,List<Integer> result){
        if(root==null){
            return;
        }
        postOrder(root.left,result);
        postOrder(root.right,result);
        result.add(root.val);
    }
}方法二:迭代法
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        Stack<TreeNode> st =new Stack<>();
        if (root==null) return result;
        TreeNode cur=root;
        while(cur!=null||!st.isEmpty()){
            if(cur!=null){
                st.push(cur);
                cur=cur.left;
            }
            else{//如果遍历到空节点,就把cur指向栈顶元素
                cur=st.pop();
                result.add(cur.val);
                cur=cur.right;
            }
        }
        return result;
    }
}94.二叉树的中序遍历
        给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
示例 1:

输入:root = [1,null,2,3]
输出:[1,3,2]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
问题分析:
①递归法
②迭代法:
利用前序遍历类似思路 入栈:中左右->出栈:中右左->翻转数组:左右中
方法一:递归法
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        inOrder(root,result);
        return result;
    }
    public void inOrder(TreeNode root,List<Integer> result){
        if(root==null){
            return;
        }
        inOrder(root.left,result);
        result.add(root.val);
        inOrder(root.right,result);
    }
}方法二:迭代法
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        Stack<TreeNode> st=new Stack<>();//栈存树节点
        if (root==null) return result;
        st.push(root);//先把root加入栈中
        while(!st.isEmpty()){
            TreeNode node=st.pop();
            result.add(node.val);
            if(node.left!=null){
                st.push(node.left);
            }
            if(node.right!=null){
                st.push(node.right);
            }
        }
    Collections.reverse(result);//static void reverse(List<?> list)反转指定列表中元素的顺序。
        return result;
    }
}



















