leetcode二叉树前、中序、后序和锯齿形遍历(非递归)——144/94/145/102/103

本文详细介绍了二叉树的四种主要遍历方法:前序遍历、中序遍历、后序遍历和层次遍历。通过代码实例展示了如何使用Java实现这些遍历,并给出了每种遍历的访问顺序和结果。此外,还提及了锯齿形层序遍历的实现思路,利用广度优先搜索和双端队列完成。
摘要由CSDN通过智能技术生成

一、前序遍历 144

https://www.cnblogs.com/zhi-leaf/p/10813048.html

1.原理

在这里插入图片描述
前序遍历的访问顺序:先根节点,再左子树,最后右子树;上图的访问结果为:GDAFEMHZ。
注:前序遍历会沿着根节点的左节点找到底,没找到然后再沿着右节点的左节点找到底,然后自底向上返回右节点

2.代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;

        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while(cur!=null || !stack.isEmpty()){
            while(cur!=null){
                //栈里存储的就是谦虚连理的结果,所以只要跟着栈一步步将结果存到List里就可以了
                res.add(cur.val);
                stack.add(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            cur = cur.right;
            }
        return res;
        }
    }

二、中序遍历 94

1.原理

中序遍历的访问顺序:先左子树,再根节点,最后右子树;上图的访问结果为:ADEFGHMZ。

2.代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        if (root == null) return res;

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur =root;

        while(cur!=null || !stack.isEmpty()){
            //入栈,依次加入cur和对应的cur.left
            while(cur!=null){
                stack.add(cur);
                cur = cur.left;
            }
            //加元素,回退
            cur = stack.pop();
            res.add(cur.val);
            cur = cur.right;

        }
        return res;
    }
}

三、后序遍历 145

1.原理

后序遍历的访问顺序:先左子树,再右子树,最后根节点,上图的访问结果为:AEFDHZMG。

2.代码

https://www.bilibili.com/video/BV1mC4y147Q5?from=search&seid=6558741197542271320&spm_id_from=333.337.0.0

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List <Integer> list = new ArrayList<>();
        if (root == null) return list;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre =null;

        while (!stack.isEmpty() || root !=null){
            if (root !=null){
                stack.push(root);
                root=root.left;
            }else{
                TreeNode peek = stack.peek();
            if (peek.right == null || pre == peek.right ) {
                list.add(peek.val);
                pre = stack.pop();
            }else{
                root = peek.right;
            }
            }

        }
        return list;
    }
}

四、层次遍历 102. 二叉树的层序遍历

1.原理

层次遍历的访问结果:GDMAFHZE

2.代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        //base case
        if (root == null) return res;

        //中间变量
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        //
        while(!queue.isEmpty()){
            ArrayList<Integer> temp = new ArrayList<>();
            int size = queue.size();
            for(int i =1; i<=size;i++){
                TreeNode cur = queue.poll();
                temp.add(cur.val);
                if (cur.left!=null) queue.add(cur.left);
                if (cur.right!=null) queue.add(cur.right);
            }
            res.add(temp);
        }
        return res;

    }
}

五、锯齿遍历 103. 二叉树的锯齿形层序遍历

1.题目

https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/

2.思路

广度优先算法(BFS)h和深度优先算法(DFS)的原理及实现见:https://blog.csdn.net/Xiao__Bei/article/details/121645498
同一加到一个队列里,啊然后用双端队列实现不同方向的取操作

3.代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        //层序遍历+双端队列
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if (root == null) return res;

        Queue<TreeNode> queue =new LinkedList<TreeNode>();
        queue.offer(root);
        boolean judge = true;

        while(!queue.isEmpty()){
            int size = queue.size();
            //List<Integer> temp = new ArrayList<>();
            Deque<Integer> deque = new LinkedList<>();
            
            for (int i = 1; i<=size; i++){
                TreeNode cur = queue.poll();
                if(judge){
                    //向队尾加元素
                    deque.offerLast(cur.val);
                }else{
                    //往队头加元素
                    deque.offerFirst(cur.val);
                }
                if(cur.left!=null) queue.offer(cur.left);
                if(cur.right!=null) queue.offer(cur.right);

                    }
            res.add(new LinkedList<Integer>(deque));
            judge = !judge;
            }
        return res;

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值