首页 > 其他分享 >【刷题笔记】111. Minimum Depth of Binary Tree

【刷题笔记】111. Minimum Depth of Binary Tree

时间:2023-11-16 23:02:04浏览次数:42  
标签:Binary minDepth TreeNode int Tree Depth return root 节点

题目

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.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

		3
   / \
  9  20
    /  \
   15   7

return its minimum depth = 2.

题目大意

给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明: 叶子节点是指没有子节点的节点。

解题思路

  • 递归求出根节点到叶子节点的深度,输出最小值即可

参考代码

package leetcode

import (
	"github.com/halfrost/LeetCode-Go/structures"
)

// TreeNode define
type TreeNode = structures.TreeNode

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

func minDepth(root *TreeNode) int {
	if root == nil {
		return 0
	}
	if root.Left == nil {
		return minDepth(root.Right) + 1
	}
	if root.Right == nil {
		return minDepth(root.Left) + 1
	}
	return min(minDepth(root.Left), minDepth(root.Right)) + 1
}

func min(a int, b int) int {
	if a > b {
		return b
	}
	return a
}

标签:Binary,minDepth,TreeNode,int,Tree,Depth,return,root,节点
From: https://blog.51cto.com/u_16110811/8433696

相关文章

  • CART(Classification and Regression Trees)
    CART(ClassificationandRegressionTrees)是一种常用的决策树算法,既可以用于分类问题,也可以用于回归问题。CART算法由Breiman等人于1984年提出,是一种基于递归二分划分的贪婪算法。以下是对CART算法的详细解释:1.决策树的构建过程:CART算法通过递归地将数据集划分为越来越纯的子集......
  • Solution - Hossam and (sub-)palindromic tree
    又名:《最近vjudge题全部罚坐》。唯一Trick:回文序列,就想区间dp!时间复杂度\(O(n^2)\)!如果是序列:\(f_{l,r}\)表示\([l,r]\)的最长回文子序列,\(f_{l,r}=\max(f_{l+1,r},f_{l,r-1},f_{l+1,r-1}+[s_l=s_r])\),\(s\)是字符串。很trivial。但是这是在......
  • antd的tree的核心显示属性
     树形组件的概念 ......
  • 【刷题笔记】110. Balanced Binary Tree
    题目Givenabinarytree,determineifitisheight-balanced.Forthisproblem,aheight-balancedbinarytreeisdefinedas:abinarytreeinwhichthedepthofthetwosubtreesofeverynodeneverdifferbymorethan1.Example1:Giventhefollowingtree......
  • CF570D Tree Requests
    题意给定一棵根为\(1\)的有根树,以及字符串\(S\)。\(x,h\)求\(x\)的子树内,深度为\(h\)的节点的字符能否重排为一个回文串。Sol不难发现,回文串显然至多有一个字符出现奇数个。所以我们对于每种字符随机附权值,维护前缀异或值。查询时枚举\(26\)种为奇数的情况,这是......
  • python tkinter treeview 仿 excel表格
    代码:fromtkinterimportttkfromtkinterimport*root=Tk()#初始框的声明columns=("姓名","IP地址")treeview=ttk.Treeview(root,height=18,show="headings",columns=columns)#表格treeview.column("姓名",width=100,a......
  • 决策树(Decision Tree)
    决策树是一种基于树结构的分类和回归模型,它通过对数据进行逐步的分解,从根节点开始,根据不同的特征进行分割,最终到达叶节点,叶节点对应一个预测结果。以下是决策树的基本概念和构建过程的详细解释:决策树的基本概念:节点(Node):根节点(RootNode):树的起始节点,包含整个数据集。内部节......
  • TreeSet
      ......
  • AT_tdpc_tree 木
    题目描述:给定一棵大小为\(n\)的树,用另外\(n\)个点加边构造出这棵树,要求构造时所被边连到的点联通,求有多少连边顺序。数据范围:\(1\leqn\leq1000\)。思路:首先我们发现,因为题目要求连边后一定是一个连通块,所以考虑以哪一个点作为起点,然后向下连边。所以我们得到一个初......
  • element-ui tree 异步树实现勾选自动展开、指定展开、指定勾选
    element-uitree异步树实现勾选自动展开、指定展开、指定勾选 背景项目中用到了vue的element-ui框架,用到了el-tree组件。由于数据量很大,使用了数据懒加载模式,即异步树。异步树采用复选框进行结点选择的时候,没法自动展开,官方文档找了半天也没有找到好的办法!找不到相关的配......