国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LeetCode 111. Minimum Depth of Binary Tree

2019-11-08 02:56:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

題目鏈接:https://leetcode.com/PRoblems/minimum-depth-of-binary-tree/?tab=Description

題目描述:

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.

思路:這道題和 LeetCode 104. Maximum Depth of Binary Tree相反, 可以用同樣的思路,但是要注意的是,當(dāng)前節(jié)點(diǎn)左子樹或者右子樹為空時(shí)需要判斷一下

代碼:

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int minDepth(TreeNode* root) {        if(root==NULL)            return 0 ;        int depth1=minDepth(root->left);        int depth2=minDepth(root->right);        if(depth1==0&&depth2==0)            return 1;        else if(depth1==0&&depth2!=0)            return depth2+1;        else if(depth1!=0&&depth2==0)            return depth1+1;        else            return depth1<depth2?depth1+1:depth2+1;            }};


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 昭觉县| 无锡市| 玛纳斯县| 荥经县| 房产| 鹤山市| 永顺县| 普宁市| 丰原市| 大港区| 南江县| 武穴市| 江华| 安塞县| 耒阳市| 五华县| 汤原县| 驻马店市| 贵溪市| 丰顺县| 元江| 资溪县| 泌阳县| 廉江市| 长岭县| 拉萨市| 同仁县| 红桥区| 武功县| 通海县| 西畴县| 富民县| 天门市| 留坝县| 望奎县| 大渡口区| 连山| 册亨县| 内江市| 蓬莱市| 望奎县|