《剑指offer》面试题:二叉树的遍历总结

本文深入探讨二叉树的遍历方法,包括前序、中序、后序及层序遍历的递归与非递归实现,提供详细的Java代码示例,帮助读者全面理解二叉树遍历原理。

二叉树的遍历总结


二叉树是一种非常常用的数据结构,也是面试的热门词。而二叉树最常见的考点莫过于遍历,剑指offer的第60页介绍树时也着重强调了二叉树遍历的重要性,但书中并未实现。本文将完整地实现二叉树遍历。

方法名称主要功能更呢
preorderRecursively前序遍历递归版
inorderRecursively中序遍历递归版
postorderRecursively后序遍历递归版
preorderIteratively前序遍历非递归版
inorderIteratively中序遍历非递归版
postorderIteratively后序遍历非递归版
levelorder层序遍历/宽度优先遍历

Java参考代码如下:

package chapter2;
import java.util.*;

public class P60_TraversalOfBinaryTree {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val){
            this.val=val;
            this.left=null;
            this.right=null;
        }
    }
    //前序遍历递归版
    public static ArrayList<Integer> preorderRecursively(TreeNode root){//递归
        ArrayList<Integer> result=new ArrayList<>();
        preoder(root,result);
        return result;
    }
    public static void preoder(TreeNode root,ArrayList<Integer> result){
        if(root==null) return;
        result.add(root.val);
        if(root.left!=null) preoder(root.left,result);
        if(root.right!=null) preoder(root.right,result);
    }

    //中序遍历递归版
    public static ArrayList<Integer> inorderRecursively(TreeNode root){
        ArrayList<Integer> result=new ArrayList<>();
        inorder(root,result);
        return result;
    }
    public static void inorder(TreeNode root,ArrayList<Integer> result){//递归
        if(root==null) return;
        if(root.left!=null) inorder(root.left,result);
        result.add(root.val);
        if(root.right!=null) inorder(root.right,result);
    }

    //后序遍历递归版
    public static ArrayList<Integer> postorderRecursively(TreeNode root){
        ArrayList<Integer> result=new ArrayList<>();
        postorder(root,result);
        return result;
    }
    public static void postorder(TreeNode root,ArrayList<Integer> result){//递归
        if(root==null) return;
        if(root.left!=null) postorder(root.left,result);
        if(root.right!=null) postorder(root.right,result);
        result.add(root.val);
    }

    //前序遍历非递归版
    public static ArrayList<Integer> preorderIteratively(TreeNode root){//非递归
        ArrayList<Integer> result=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();//ArrayDeque.addFirst\.pollFirst
        TreeNode p=root;
        while (!stack.isEmpty()||p!=null){
            if(p!=null){
                stack.add(p);
                result.add(p.val);
                p=p.left;
            }else {
                TreeNode t=stack.pop();
                p=t.right;
            }
        }
        return result;
    }

    //中序遍历非递归版
    public static ArrayList<Integer> inorderIteratively(TreeNode root){//非递归
        ArrayList<Integer> result=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();//ArrayDeque.addFirst\.pollFirst
        TreeNode p=root;
        while(!stack.isEmpty()||p!=null){
            if(p!=null){
                stack.add(p);
                p=p.left;
            }else {
                TreeNode t=stack.pop();
                result.add(t.val);
                p=t.right;
            }
        }
        return result;
    }

    //后序遍历非递归版
    public static ArrayList<Integer> postorderIteratively(TreeNode root){//非递归
        ArrayList<Integer> result=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();//ArrayDeque.addFirst\.pollFirst
        TreeNode p=root;
        while(!stack.isEmpty()||p!=null){
            if(p!=null){
                stack.add(p);
                result.add(0,p.val);//从头插入,可用ArrayDeque,addFirst快。
                p=p.right;
            }else {
                TreeNode t=stack.pop();
                p=t.left;
            }
        }
        return result;
    }

    //层序遍历
    public static ArrayList<Integer> levelorder(TreeNode root){
        ArrayList<Integer> result=new ArrayList<>();
        ArrayDeque<TreeNode> queue=new ArrayDeque<>();
        queue.addLast(root);
        while (!queue.isEmpty()){
        	//for(int i=queue.size();i>=0;i--)//level
            TreeNode p=queue.pollFirst();
            result.add(p.val);
            if(p.left!=null)
                queue.addLast(p.left);
            if(p.right!=null)
                queue.addLast(p.right);
        }
        return result;
    }

    public static void main(String[] args){
        TreeNode root=new TreeNode(1);
        root.left=new TreeNode(2);
        root.right=new TreeNode(3);
        root.left.left=new TreeNode(4);
        root.left.right=new TreeNode(5);
        root.right.left=new TreeNode(6);
        root.right.right=new TreeNode(7);

        List<Integer> list_preorderRecursively = preorderRecursively(root);
        System.out.print("preorderRecursively: "+'\t');
        System.out.println(list_preorderRecursively.toString());

        List<Integer> list_inorderRecursively = inorderRecursively(root);
        System.out.print("inorderRecursively: "+'\t');
        System.out.println(list_inorderRecursively.toString());

        List<Integer> list_postorderRecursively = postorderRecursively(root);
        System.out.print("postorderRecursively: "+'\t');
        System.out.println(list_postorderRecursively.toString());
        System.out.println();


        List<Integer> list_preorderIteratively = preorderIteratively(root);
        System.out.print("preorderIteratively: "+'\t');
        System.out.println(list_preorderIteratively.toString());

        List<Integer> list_inorderIteratively = inorderIteratively(root);
        System.out.print("inorderIteratively: "+'\t');
        System.out.println(list_inorderIteratively.toString());

        List<Integer> list_postorderIteratively = postorderIteratively(root);
        System.out.print("postorderIteratively: "+'\t');
        System.out.println(list_postorderIteratively.toString());
        System.out.println();

        List<Integer> list_levelorder = levelorder(root);
        System.out.print("levelorder: "+'\t');
        System.out.println(list_levelorder.toString());
    }
}


参考:
https://www.jianshu.com/p/362d4ff42ab2
http://www.cnblogs.com/grandyang/p/4146981.html
http://www.cnblogs.com/grandyang/p/4297300.html

该文档【DeepSeek教育机器人智能化方案:基于多模态感知技术的教育场景理解与自然对话交互系统】共计 909 页,共51个大章节,文档支持目录章节跳转同时还支持阅读器左侧书签大纲显示和章节快速定位,文档内容完整、条理清晰。文档内所有文字、图表、目录等元素均显示正常,无任何异常情况,敬请您放心查阅与使用。文档仅供学习参考,请勿用作商业用途。文档前18个章节内容:【引言:DeepSeek教育机器人智能化的核心诉求与技术突破口、教育场景多模态据特征解析:文本、语音、视觉据的行业特殊性、多模态感知技术在教育场景的适配原则:低延迟、高精准、强容错、DeepSeek多模态感知技术架构核心:模态融合的底层逻辑设计、教育文本据预处理技术:结构化与非结构化文本的清洗与标准化、教育场景语音据采集规范:课堂环境下的降噪与收音优化方案、视觉据预处理技术:学生行为、表情、姿态据的去冗余方法、多模态据对齐技术:时间戳同步与语义关联的双向映射实现、教育领域据标注规范设计:文本意图、语音情感、视觉行为的标注体系、据标注质量控制机制:多轮校验与标注一致性校验算法、小样本据标注增强技术:基于迁移学习的标注效率提升方案、教育专用语料库构建:学科术语、教学场景话术的结构化存储、语音情感标注体系:课堂互动中积极/消极/中性情感的细粒度标注、学生行为视觉标注规范:专注、走神、互动等行为的特征定义与标注、多模态标注工具选型与定制化开发:适配教育场景的标注流程优化、文本模态预训练模型选型:DeepSeek-R1在教育文本理解的适配改造、语音识别预训练模型优化:教育场景方言、童声、课堂噪音的适配训练、视觉识别预训练模型初始化:基于DeepSeek-VL的教育场景迁移训练】。更多精品资源请访问 https://blog.csdn.net/ashyyyy/article/details/146464041
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值