Java二叉树的前序,中序,后序遍历的递归与迭代写法

二叉树的遍历
前中后是指根节点被访问的顺序,左节点永远先于右节点被访问
前序遍历:根-左-右
中序遍历:左-根-右
后序遍历:左-右-根
如下图所示
Java二叉树的前序,中序,后序遍历的递归与迭代写法_第1张图片
递归解法
递归算法三要素:
1,确定递归函数的参数和返回值。
2,确定终止条件。
3,确定单层递归的逻辑。
其实就是模板,改变访问顺序
前序递归

public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        dfs(root, res);
        return res;
    }
    public void dfs(TreeNode root, List<Integer> res) {
        if(root == null) return;
        res.add(root.val);
        dfs(root.left,res);
        dfs(root.right,res);
    }

后序递归

public void dfs(TreeNode root, List<Integer> res) {
        if(root == null) return;
        dfs(root.left,res);
        dfs(root.right,res);
        res.add(root.val);
    }

中序遍历

public void dfs(TreeNode root, List<Integer> res) {
        if(root == null) return;
        dfs(root.left,res);
        res.add(root.val);
        dfs(root.right,res);
    }

迭代解法
通常用栈来实现DFS的迭代遍历
前序迭代,放入根节点后,要先放入右节点再放左节点,这样出栈顺序才能变成中-左-右

1,push root to stack
2, cur = stack.pop()
print(cur.val)
3,push cur.right to stack
4,push cur.left to stack

public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;
        Deque<TreeNode> stack = new ArrayDeque<>();
        stack.offerFirst(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pollFirst();
            res.add(node.val);
            if (node.right != null) {
                stack.offerFirst(node.right);
            }
            if (node.left != null) {
                stack.offerFirst(node.left);
            }
        }
        return res;
    }

中序迭代

1,loop:while root not null
push root to stack
root = root.left
2, root = stack.pop()
print(root.val)
3, root = root.right

public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;
        Deque<TreeNode> stack = new ArrayDeque<>();
        while (!stack.isEmpty() || root != null) {
            while (root != null) {
                stack.offerFirst(root);
                root = root.left;
            }
            root = stack.pollFirst();
            res.add(root.val);
            root = root.right;
        }
        return res;
    }

后序迭代

前序遍历顺序为 root-left-right ==>将left和right的访问顺序对调 ==> root-right-left ==> 反转 ==> left-right-root(后序)
因为要反转顺序,用LinkedList来存储数据,而不是ArrayList
1, push root to stack
2, cur = stack.pop()
addFirst(cur.val)
3, push cur.left to stack
4, push cur.right to stack

public List<Integer> postorderTraversal(TreeNode root) {
        LinkedList<Integer> res = new LinkedList<>();
        if (root == null) return res;
        Deque<TreeNode> stack = new ArrayDeque<>();
        stack.offerFirst(root);
        while (!stack.isEmpty()) {
            root = stack.pollFirst();
            res.addFirst(root.val);
            if (root.left != null) {
                stack.offerFirst(root.left);
            }
            if (root.right != null) {
                stack.offerFirst(root.right);
            }
        }
        return res;
    }

你可能感兴趣的:(二叉树,dfs,stack)