二叉树的递归遍历|前中后序遍历、最大深度、最大直径

二叉树的递归遍历

前序遍历

java 复制代码
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();

        if (root == null) {
            return res;
        }

        res.add(root.val);
        if (root.left != null) {
            res.addAll(preorderTraversal(root.left));
        }
        if (root.right != null) {
            res.addAll(preorderTraversal(root.right));
        }
        return res;
    }

中序遍历

java 复制代码
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();

        if (root == null) {
            return res;
        }

        if (root.left != null) {
            res.addAll(inorderTraversal(root.left));
        }
        res.add(root.val);
        if (root.right != null) {
            res.addAll(inorderTraversal(root.right));
        }
        return res;
    }

后序遍历

java 复制代码
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();

        if (root == null) {
            return res;
        }

        if (root.left != null) {
            res.addAll(postorderTraversal(root.left));
        }
        if (root.right != null) {
            res.addAll(postorderTraversal(root.right));
        }
        res.add(root.val);
        return res;
    }

层序遍历

java 复制代码
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) {
            return res;
        }
        helper(root, 0);
        return res;
    }

    private void helper(TreeNode root, int level) {
        if (level == res.size()) {
            res.add(new ArrayList<>());
        }
        res.get(level).add(root.val);

        if (root.left != null) {
            helper(root.left, level+1);
        }
        if (root.right != null) {
            helper(root.right, level+1);
        }
    }

最大深度

java 复制代码
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }

最大直径

java 复制代码
    int maxd = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }
        depth(root);
        return maxd;
    }

    private int depth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int left = depth(root.left);
        int right = depth(root.right);
        maxd = Math.max(left+right, maxd); //获得当前节点的直径为 左右子树深度和
        return Math.max(left, right) + 1; //当前节点的深度为左右子树深度的最大值+1
    }
相关推荐
2501_9167665416 小时前
【Springboot】数据层开发-数据源自动管理
java·spring boot·后端
自在极意功。17 小时前
MyBatis 动态 SQL 详解:从基础到进阶实战
java·数据库·mybatis·动态sql
软件管理系统17 小时前
基于Spring Boot的便民维修管理系统
java·spring boot·后端
源代码•宸17 小时前
Leetcode—620. 有趣的电影&&Q3. 有趣的电影【简单】
数据库·后端·mysql·算法·leetcode·职场和发展
百***787517 小时前
Step-Audio-2 轻量化接入全流程详解
android·java·gpt·php·llama
快乐肚皮18 小时前
MySQL递归CTE
java·数据库·mysql·递归表达式
廋到被风吹走18 小时前
【Spring】DispatcherServlet解析
java·后端·spring
廋到被风吹走18 小时前
【Spring】PlatformTransactionManager详解
java·spring·wpf
wanghowie18 小时前
01.07 Java基础篇|函数式编程与语言新特性总览
java·开发语言·面试