題目
原題頁面:https://leetcode.com/problems/minimum-depth-of-binary-tree/
題目型別:Tree, Depth-first Search, Breadth-first Search
難度評價:Easy
類似題目:(E) Binary Tree Level Order Traversal, (E) Meeting Rooms, (E) Maximum Depth of Binary Tree
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.
分析
計算二叉樹的最小深度。好像還有另外一道題是計算二叉樹的最大深度,其實解法都類似,遞迴加回溯。
但是要注意一點的是,如果一個node的左子樹高度是2,右子樹高度是0,則說明node是沒有右節點的,此時node為root的子樹的最小高度應該是2+1=3,而不應該是0+1=1。
程式碼
1 | class Solution: |
本文地址:https://xnerv.wang/zh-tw/leetcode-minimum-depth-of-binary-tree/
