C++實(shí)現(xiàn)LeetCode(111.二叉樹(shù)的最小深度)
[LeetCode] 111. Minimum Depth of Binary Tree 二叉樹(shù)的最小深度
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.
二叉樹(shù)的經(jīng)典問(wèn)題之最小深度問(wèn)題就是就最短路徑的節(jié)點(diǎn)個(gè)數(shù),還是用深度優(yōu)先搜索 DFS 來(lái)完成,萬(wàn)能的遞歸啊。首先判空,若當(dāng)前結(jié)點(diǎn)不存在,直接返回0。然后看若左子結(jié)點(diǎn)不存在,那么對(duì)右子結(jié)點(diǎn)調(diào)用遞歸函數(shù),并加1返回。反之,若右子結(jié)點(diǎn)不存在,那么對(duì)左子結(jié)點(diǎn)調(diào)用遞歸函數(shù),并加1返回。若左右子結(jié)點(diǎn)都存在,則分別對(duì)左右子結(jié)點(diǎn)調(diào)用遞歸函數(shù),將二者中的較小值加1返回即可,參見(jiàn)代碼如下:
解法一:
class Solution { public: int minDepth(TreeNode* root) { if (!root) return 0; if (!root->left) return 1 + minDepth(root->right); if (!root->right) return 1 + minDepth(root->left); return 1 + min(minDepth(root->left), minDepth(root->right)); } };
我們也可以是迭代來(lái)做,層序遍歷,記錄遍歷的層數(shù),一旦遍歷到第一個(gè)葉結(jié)點(diǎn),就將當(dāng)前層數(shù)返回,即為二叉樹(shù)的最小深度,參見(jiàn)代碼如下:
解法二:
class Solution { public: int minDepth(TreeNode* root) { if (!root) return 0; int res = 0; queue<TreeNode*> q{{root}}; while (!q.empty()) { ++res; for (int i = q.size(); i > 0; --i) { auto t = q.front(); q.pop(); if (!t->left && !t->right) return res; if (t->left) q.push(t->left); if (t->right) q.push(t->right); } } return -1; } };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/111
類(lèi)似題目:
Binary Tree Level Order Traversal
參考資料:
https://leetcode.com/problems/minimum-depth-of-binary-tree/
https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/36153/My-concise-c%2B%2B-solution
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(111.二叉樹(shù)的最小深度)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)二叉樹(shù)的最小深度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(116.每個(gè)節(jié)點(diǎn)的右向指針)
- C++實(shí)現(xiàn)LeetCode(115.不同的子序列)
- C++實(shí)現(xiàn)LeetCode(114.將二叉樹(shù)展開(kāi)成鏈表)
- C++實(shí)現(xiàn)LeetCode(110.平衡二叉樹(shù))
- C++實(shí)現(xiàn)LeetCode(141.單鏈表中的環(huán))
- C++實(shí)現(xiàn)LeetCode(142.單鏈表中的環(huán)之二)
- C++實(shí)現(xiàn)LeetCode(143.鏈表重排序)
- C++實(shí)現(xiàn)LeetCode(121.買(mǎi)賣(mài)股票的最佳時(shí)間)
相關(guān)文章
C語(yǔ)言詳解如何實(shí)現(xiàn)帶頭雙向循環(huán)鏈表
帶頭雙向循環(huán)鏈表:結(jié)構(gòu)最復(fù)雜,一般用在單獨(dú)存儲(chǔ)數(shù)據(jù)。實(shí)際中使用的鏈表數(shù)據(jù)結(jié)構(gòu),都是帶頭雙向循環(huán)鏈表。另外這個(gè)結(jié)構(gòu)雖然結(jié)構(gòu)復(fù)雜,但是使用代碼實(shí)現(xiàn)以后會(huì)發(fā)現(xiàn)結(jié)構(gòu)會(huì)帶來(lái)很多優(yōu)勢(shì),實(shí)現(xiàn)反而簡(jiǎn)單2022-04-04C++實(shí)現(xiàn)數(shù)據(jù)文件存儲(chǔ)與加載
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)數(shù)據(jù)文件存儲(chǔ)與加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06C++二叉樹(shù)實(shí)現(xiàn)詞頻分析功能
這篇文章主要為大家詳細(xì)介紹了C++二叉樹(shù)實(shí)現(xiàn)詞頻分析功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之學(xué)生信息管理系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之學(xué)生信息管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11C語(yǔ)言的字符函數(shù)和字符串函數(shù)詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言的字符函數(shù)和字符串函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03C語(yǔ)言詳解Z字形變換排列的實(shí)現(xiàn)
Z字形變換排列就是指將一個(gè)給定字符串根據(jù)給定的行數(shù),以從上往下、從左到右進(jìn)行 Z 字形排列,下面讓我們用C語(yǔ)言來(lái)實(shí)現(xiàn)2022-04-04