279 字
1 分钟
LeetCode-111.二叉树的最小深度
二叉树
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
**说明:**叶子节点是指没有子节点的节点。
示例 1:
输入:root = [3,9,20,null,null,15,7]输出:2示例 2:
输入:root
自己的解法
思路:这题与 二叉树的最小深度 稍有不同,求最小时会面临一个问题:如果一颗树是“偏瘫”的(比如只有左边的,没有右边的),直接用 min(left, right),右边的空指针会返回深度 0,导致整棵树算出来的最小深度是 1。
所以,我们需要if root.Left == nil && root.Right != nil { ... } 和
if root.Right == nil && root.Left != nil { ... } 去除这些错误
/** * 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 && root.Right != nil { return minDepth(root.Right) + 1 }
if root.Right == nil && root.Left != nil { return minDepth(root.Left) + 1 }
return min(minDepth(root.Left), minDepth(root.Right)) + 1} LeetCode-111.二叉树的最小深度
https://sheep44044.github.io/posts/算法/二叉树/leetcode-111二叉树的最小深度/
