[Golang][Leetcode][BinaryTree]刷題系列-104-Minimum Depth of Binary Tree

Contents
111. Minimum Depth of Binary Tree
Level : Easy
原題連結 : Click
題目 :
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 :
Note
Example 1:
Input: root = [3,9,20,null,null,15,7] Output: 2
Example 2:
Input: root = [2,null,3,null,4,null,5,null,6] Output: 5
解題思路 :
- 這題的解法可以參考 leetcode104題解,我使用同樣queue的解法,只是多做了一個判斷當有一個node沒有left children & right children時,直接回傳depth,因為那便是最小深度
queue解法
- time complexity: O(n) , space complexity: O(n)
Runtime: 184 ms, faster than 98.29% of Go online submissions for Minimum Depth of Binary Tree.
|
|
最後祝福努力認真的各位 “All your dream of are hidden in your daily life” 我們峰頂見!!!