LeetCode Minimum Depth of Binary Tree

news/2025/2/25 18:58:31

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

思路分析:这题和Maximum Depth of Binary Tree相似,可是如今要返回最小深度,递归的解法须要加一个特殊的推断。就是假设某个node比方root仅仅有一个孩子,这时不能返回最小深度是0。由于仅仅有在叶子节点处(而不是空处)才干计算深度,所以当左孩子为空,传入右孩子递归调用;当右孩子为空,传入左孩子递归调用。

而在Maximum Depth of Binary Tree没有这个问题,由于我们是求最大深度,某个node仅仅有一个孩子,我们会计深度为1而不是0。

递归解法例如以下。多加了对仅仅有一个孩子的情况的推断:

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) return 0;
        if(root.left == null) return minDepth(root.right) + 1;
        if(root.right == null) return minDepth(root.left) + 1;
        return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
    }
}

非递归解法借助队列进行BFS计算深度。当遇到第一个叶子节点返回深度就可以,最后加了一个return 0保证返回出口,事实上叶子节点必定存在。一定会从while循环里面就返回。

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) return 0;
        LinkedList<TreeNode> treeNodeQueue = new LinkedList<TreeNode>();
        int level = 1;
        treeNodeQueue.add(root);
        int curLevelNum = 1;
        int nextLevelNum = 0;
        while(!treeNodeQueue.isEmpty()){
            TreeNode curNode = treeNodeQueue.poll();//find and remove; different from peek
            if(curNode.left == null && curNode.right == null) return level;
            curLevelNum--;
            if(curNode.left != null){
                treeNodeQueue.add(curNode.left);
                nextLevelNum++;
            }
            if(curNode.right != null){
                treeNodeQueue.add(curNode.right);
                nextLevelNum++;
            }
            if(curLevelNum == 0){
                level++;
                curLevelNum = nextLevelNum;
                nextLevelNum = 0;//added a level
            }
        }
        return 0;
    }
}


版权声明:本文博客原创文章。博客,未经同意,不得转载。


http://www.niftyadmin.cn/n/710564.html

相关文章

大二上学期九月周总结报告二

这周我在慕课网上学习java的入门课程&#xff0c;重新回顾在暑假学习过的java的类的定义、对象的使用、静态和动态方法等&#xff0c;编写了一个简单的成绩信息管理系统&#xff0c; 并且安装好了mysql和配置好了环境。 下周继续学习java基础课程&#xff0c;学习建模语言基础知…

91 数星星

91 数星星 一闪一闪亮晶晶&#xff0c;漫天都是小星星挂在天上放光明&#xff0c;好像许多小眼睛 作者: Turbo时间限制: 1S章节: 结构体 问题描述 : 一天&#xff0c;小明坐在院子里数星星&#xff0c;爸爸就出了个难题给她&#xff1a;爸爸在天空指定了一个区域&#xff0…

学号:201621123032 《Java程序设计》第9周学习总结(

1&#xff1a;本周学习总结 1.1&#xff1a;以你喜欢的方式&#xff08;思维导图或其他&#xff09;归纳总结集合与泛型相关内容 2&#xff1a;书面作业 2.1&#xff1a; List中指定元素的删除(题集题目) 2.1.1&#xff1a;实验总结。并回答&#xff1a;列举至少2种在List中删除…

seci-log 1.03 日志分析软增加web日志分析

日志分析软件的升级了&#xff0c;我们在上次十种告警(非上班时间访问&#xff0c;非上班地点访问&#xff0c;密码猜测&#xff0c;账号猜测&#xff0c;账号猜测成功、敏感文件操作告警和高危命令操作&#xff0c;主机扫描&#xff0c;端口扫描&#xff0c;非法外联)的基础上…

92 按出生日期排序

92 按出生日期排序 作者: Turbo时间限制: 1S章节: 结构体 问题描述 : 小明希望将自己的通讯录按好友的生日顺序排序&#xff0c;这样查看起来方便多了&#xff0c;也避免错过好友的生日。 为了小明的美好愿望&#xff0c;你帮帮他吧。小明的好友信息包含姓名、出生日期。其…

vue 动态组件的传值

vue项目开发中会用到大量的父子组件传值&#xff0c;也会用到动态组件的传值&#xff0c;常规子组件获取父组件的传值时&#xff0c;第一次是获取不到的&#xff0c;这时候有两种解决方案 第一种&#xff1a; 父组件向子组件传的是一个json对象,ES6的方法Object.keys() 转化成数…

Oracle 12c RAC 日志体系结构的变化

1 说明 在11g中&#xff0c;查看GRID的日志&#xff0c;会进入$ORACLE_HOM/log。 [gridcndba.cn ~]$ cd $ORACLE_HOME/log/ [gridcndba.cn log]$ ls crs diag rac1 [gridcndba.cn log]$ cd rac1 [gridcndba.cn rac1]$ ls acfs admin afd alertrac1.log client crflog…

78 字符串中找整数

78 字符串中找整数 作者: zzp时间限制: 1S章节: 字符串 问题描述 : 对于一个字符串&#xff0c;编程找出其中的所有整数。例如&#xff0c;字符串“a12bc34d05”&#xff0c;其中有整数12、34、5。 输入说明 : 程序输入包括多行&#xff0c;每一行都是一串字符&#xff0c…