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

二叉树的遍历
前中后是指根节点被访问的顺序,左节点永远先于右节点被访问
前序遍历:根-左-右
中序遍历:左-根-右
后序遍历:左-右-根
如下图所示
在这里插入图片描述
递归解法
递归算法三要素:
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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值