二叉树的前中后序遍历(Leetcode 94&&144&&145)

题目

​​​​​​144. 二叉树的前序遍历

145. 二叉树的后序遍历

94. 二叉树的中序遍历

代码

        分为三种方法1)递归遍历;2)迭代遍历;3)统一迭代遍历 

递归遍历 

public List preorderTraversal(TreeNode root) {
//        递归遍历:根节点、右子树、左子树
        List result = new ArrayList<>();
        preoder(result, root);
        return result;

    }
    public void preoder(List result, TreeNode root){
        if(root == null){
            return;
        }
        // 前序遍历 根节点、左子树、右子树
        // 中序遍历 左子树、根节点、右子树
       //  后序遍历 左子树、右子树、根节点
        result.add(root.val);
        preoder(result, root.left);
        preoder(result, root.right);
        return;
    }

 迭代遍历:使用栈来存放数据

public List preorderTraversal_1(TreeNode root) {
//    迭代遍历 进栈顺序:根节点、右子树、左子树
//     循环结束条件:栈为空
        Stack stack = new Stack<>();
        List out = new ArrayList<>();
//      对空节点进行判断!
        if(root == null){
            return out;
        }
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode temp  = stack.pop();
            out.add(temp.val);
            if(temp.right != null){
                stack.push(temp.right);
            }
            if(temp.left != null){
                stack.push(temp.left);
            }
        }
        return out;

    }

public List inorderTraversal(TreeNode root) {
//        迭代遍历 先找到整棵树最左下角的节点 出栈后考虑该节点的右边子节点
//        有点子巧妙的 自己写的把找最左的节点和出栈循环分开写了 这样就会进入死循环
        Stack stack = new Stack<>();
        List out = new ArrayList<>();
        if(root == null){
            return out;
        }
        TreeNode temp = root;
        while (temp != null || !stack.isEmpty()){
            if(temp != null){
                stack.push(temp);
                temp = temp.left;
            }
            else {
                temp = stack.pop();
                out.add(temp.val);
                temp = temp.right;
            }
        }
        return out;

    }
public List postorderTraversal(TreeNode root) {
//        迭代遍历 将前序遍历修改左右顺序再颠倒out数组 就是后序遍历
        Stack stack = new Stack<>();
        List out = new ArrayList<>();
//      对空节点进行判断!
        if(root == null){
            return out;
        }
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode temp  = stack.pop();
//           中、左、右 -> 中、右、左
            out.add(temp.val);
            if(temp.left != null){
                stack.push(temp.left);
            }
            if(temp.right != null){
                stack.push(temp.right);
            }
        }
//        翻转
        Collections.reverse(out);
        return out;

    }

统一迭代:使用null来辅助进行标记

public List preorderTraversal_2(TreeNode root) {
//        统一格式进行迭代遍历 前序遍历 :右节点、左节点、根节点
//        中序遍历 只需要将while中放入节点顺序进行交换:右节点、根节点、左节点
//        后序遍历 :根节点、左节点、右节点

        List result = new ArrayList<>();
        Stack stack = new Stack<>();
        if(root == null){
            return result;
        }
        TreeNode temp = root;
        stack.push(temp);
        while(!stack.isEmpty()){
            temp = stack.pop();
            if(temp != null){
                // 按右、左、根节点的顺序放入栈中
                if(temp.right != null){
                    stack.push(temp.right);
                }

                if(temp.left != null){
                    stack.push(temp.left);
                }
//                将当前节点使用一个null节点进行标记
                stack.push(temp);
                stack.push(null);
            }
            else {
//                当前节点为null 即将下一节点放在result数组中
                result.add(stack.pop().val);
            }
        }
        return result;

    }

你可能感兴趣的:(小菜鸡的JAVA学习,leetcode,算法,职场和发展)