《剑指offer》面试题55:二叉树的深度

本文详细介绍了二叉树最大及最小深度的计算方法,包括递归与非递归(广度优先/层序遍历)两种策略。通过具体代码实现与测试用例,帮助读者深入理解二叉树深度的计算原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目一:二叉树的深度

输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点一次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。

思路:

1.二叉树root的深度比其子树root.left与root.right的深度的最大值大1。因此可以通过上述结论递归求解。
2. 非递归方法,而是采用广度优先/层序遍历求解。
3. 此深度为二叉树最大深度,经过一些改动可求得最小深度。

基于以上思路,java参考代码如下:

package chapter6;

import java.util.ArrayDeque;

public class P271_TreeDepth {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val){
            this.val=val;
            this.left=null;
            this.right=null;
        }
    }
    //递归
    public static int treeDepth(TreeNode root){
        if(root==null)
            return 0;
        return 1+Math.max(treeDepth(root.left),treeDepth(root.right));
    }
    //非递归,广度优先/层序遍历
    public static int treeDepth2(TreeNode root){
        if(root==null) return 0;
        ArrayDeque<TreeNode> queue=new ArrayDeque<>();
        queue.addLast(root);
        int deep=0;
        while (!queue.isEmpty()){
            deep++;
            for(int i=queue.size();i>0;i--){
                TreeNode p=queue.pollFirst();
                if(p.left!=null) queue.addLast(p.left);
                if(p.right!=null) queue.addLast(p.right);
            }
        }
        return deep;
    }

    //二叉树的最小深度,问题
    //递归
    public static int mintreeDepth(TreeNode root){
        if(root==null)
            return 0;
        return 1+Math.min(treeDepth(root.left),treeDepth(root.right));
    }
    //非递归,广度优先/层序遍历
    public static int mintreeDepth2(TreeNode root){
        if(root==null) return 0;
        ArrayDeque<TreeNode> queue=new ArrayDeque<>();
        queue.addLast(root);
        int deep=0;
        while (!queue.isEmpty()){
            deep++;
            for(int i=queue.size();i>0;i--){
                TreeNode p=queue.pollFirst();
                if(p.left==null&&p.right==null) return deep;//当遇到叶子节点,即找到最小深度,返回
                if(p.left!=null) queue.addLast(p.left);
                if(p.right!=null) queue.addLast(p.right);
            }
        }
        return -1;
    }
    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);
        System.out.println(treeDepth(root));
        System.out.println(treeDepth2(root));
        System.out.println(mintreeDepth(root));
        System.out.println(mintreeDepth2(root));
    }
}

测试用例:

a.功能测试(输入普通的二叉树;二叉树所有节点都没有左或右子树)。
b.特殊输入测试(二叉树只有一个节点;二叉树的头节点为nullptr指针)。

参考:

https://www.jianshu.com/p/0411c31d0b08
http://www.cnblogs.com/grandyang/p/4042168.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值