LeetCode——二叉树遍历

时间:2020-03-07
本文章向大家介绍LeetCode——二叉树遍历,主要包括LeetCode——二叉树遍历使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

先序

递归:

    public static ArrayList<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> array = new ArrayList<>();
        if (root == null)
            return array;
        preorder(root, array);
        return array;
    }

    public static void preorder(TreeNode root, ArrayList<Integer> array) {
        if (root == null)
            return;
        array.add(root.val);
        preorder(root.left, array);
        preorder(root.right, array);
    }

非递归:

    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> array = new ArrayList<>();
        if (root == null)
            return array;
        Stack<TreeNode> s = new Stack<>();
        TreeNode node = root;
        while (node != null || !s.empty()) {
            while (node != null) {
                //遍历到左子树下面,边遍历边保存
                array.add(node.val);
                s.push(node);
                node = node.left;
            }
            if (!s.empty()) {
                node = s.peek();
                s.pop();
                //进入右子树,再在下一步遍历左子树
                node = node.right;
            }
        }
        return array;
    }

中序

递归:

    public static ArrayList<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> array = new ArrayList<>();
        if (root == null)
            return array;
        inorder(root, array);
        return array;
    }

    public static void inorder(TreeNode root, ArrayList<Integer> array) {
        if (root == null)
            return;
        inorder(root.left, array);
        array.add(root.val);
        inorder(root.right, array);
    }

非递归:

    public static ArrayList<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> array = new ArrayList<>();
        if (root == null)
            return array;
        Stack<TreeNode> s = new Stack<>();
        TreeNode node = root;
        while (node != null || !s.empty()) {
            while (node != null) {
                s.push(node);
                node = node.left;
            }
            if(!s.empty()){
                node = s.peek();
                array.add(node.val);
                s.pop();
                node = node.right;
            }
        }
        return array;
    }

后序

递归:

    public static ArrayList<Integer> postorderTraversal(TreeNode root){
        ArrayList<Integer> array = new ArrayList<>();
        if(root == null)
            return array;
        posorder(root, array);
        return array;
    }

    public static void posorder(TreeNode root, ArrayList<Integer> array){
        if(root == null)
            return;
        posorder(root.left, array);
        posorder(root.right, array);
        array.add(root.val);
    }

非递归

    public static ArrayList<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> array = new ArrayList<>();
        if (root == null)
            return array;
        Stack<TreeNode> stack = new Stack<>();
        //当前节点
        TreeNode node = root;
        //访问的前一个节点
        TreeNode last = null;
        
        while (node != null) {
            stack.push(node);
            //移到左子树最下面
            node = node.left;
        }
        while (!stack.empty()) {
            node = stack.peek();
            stack.pop();
            //如果右子树为空或右子树被访问过
            if (node.right == null || node.right == last) {
                array.add(node.val);
                last = node;
            } else {
                //重新把当前点放进去
                stack.push(node);
                //读取右子树
                node = node.right;
                while (node != null) {
                    //把右子树的左子树递归放入
                    stack.push(node);
                    node = node.left;
                }
            }
        }
        return array;
    }

层序

    public static ArrayList<Integer> levelTraversal(TreeNode root) {
        ArrayList<Integer> array = new ArrayList<>();
        if(root == null)
            return array;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        TreeNode node;
        while(!queue.isEmpty()){
            node = queue.peek();
            array.add(node.val);
            queue.poll();
            if(node.left!=null)
                queue.offer(node.left);
            if(node.right!=null)
                queue.offer(node.right);
        }
        return array;
    }

原文地址:https://www.cnblogs.com/xym4869/p/12436373.html