日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

發布時間:2025/4/16 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

LeetCode:Minimum 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.

算法1:dfs遞歸的求解

1 class Solution { 2 public: 3 int minDepth(TreeNode *root) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 if(root == NULL)return 0; 7 int res = INT_MAX; 8 dfs(root, 1, res); 9 return res; 10 } 11 void dfs(TreeNode *root, int depth, int &res) 12 { 13 if(root->left == NULL && root->right == NULL && res > depth) 14 {res = depth; return;} 15 if(root->left) 16 dfs(root->left, depth+1, res); 17 if(root->right) 18 dfs(root->right, depth+1, res); 19 } 20 }; View Code

還有一種更直觀的遞歸解法,分別求左右子樹的最小深度,然后返回左右子樹的最小深度中較小者+1

1 class Solution { 2 public: 3 int minDepth(TreeNode *root) { 4 if(root == NULL)return 0; 5 int minleft = minDepth(root->left); 6 int minright = minDepth(root->right); 7 if(minleft == 0) 8 return minright + 1; 9 else if(minright == 0) 10 return minleft + 1; 11 else return min(minleft, minright) + 1; 12 } 13 };

?

算法2:層序遍歷二叉樹,找到最先遍歷到的葉子的層數就是樹的最小高度

1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int minDepth(TreeNode *root) { 13 // IMPORTANT: Please reset any member data you declared, as 14 // the same Solution instance will be reused for each test case. 15 //層序遍歷,碰到第一個葉子節點就停止,NULL作為每一層節點的分割標志 16 if(root == NULL)return 0; 17 int res = 0; 18 queue<TreeNode*> Q; 19 Q.push(root); 20 Q.push(NULL); 21 while(Q.empty() == false) 22 { 23 TreeNode *p = Q.front(); 24 Q.pop(); 25 if(p != NULL) 26 { 27 if(p->left)Q.push(p->left); 28 if(p->right)Q.push(p->right); 29 if(p->left == NULL && p->right == NULL) 30 { 31 res++; 32 break; 33 } 34 } 35 else 36 { 37 res++; 38 if(Q.empty() == false)Q.push(NULL); 39 } 40 } 41 return res; 42 } 43 };

?本文地址


LeetCode:Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

算法1:dfs遞歸求解

1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int maxDepth(TreeNode *root) { 13 // IMPORTANT: Please reset any member data you declared, as 14 // the same Solution instance will be reused for each test case. 15 if(root == NULL)return 0; 16 int res = INT_MIN; 17 dfs(root, 1, res); 18 return res; 19 } 20 void dfs(TreeNode *root, int depth, int &res) 21 { 22 if(root->left == NULL && root->right == NULL && res < depth) 23 {res = depth; return;} 24 if(root->left) 25 dfs(root->left, depth+1, res); 26 if(root->right) 27 dfs(root->right, depth+1, res); 28 } 29 }; View Code

同上一題

?

1 class Solution { 2 public: 3 int maxDepth(TreeNode *root) { 4 if(root == NULL)return 0; 5 int maxleft = maxDepth(root->left); 6 int maxright = maxDepth(root->right); 7 if(maxleft == 0) 8 return maxright + 1; 9 else if(maxright == 0) 10 return maxleft + 1; 11 else return max(maxleft, maxright) + 1; 12 } 13 };

?

算法2:層序遍歷,樹的總層數就是樹的最大高度

1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int maxDepth(TreeNode *root) { 13 // IMPORTANT: Please reset any member data you declared, as 14 // the same Solution instance will be reused for each test case. 15 //層序遍歷計算樹的層數即可,NULL作為每一層節點的分割標志 16 if(root == NULL)return 0; 17 int res = 0; 18 queue<TreeNode*> Q; 19 Q.push(root); 20 Q.push(NULL); 21 while(Q.empty() == false) 22 { 23 TreeNode *p = Q.front(); 24 Q.pop(); 25 if(p != NULL) 26 { 27 if(p->left)Q.push(p->left); 28 if(p->right)Q.push(p->right); 29 } 30 else 31 { 32 res++; 33 if(Q.empty() == false)Q.push(NULL); 34 } 35 } 36 return res; 37 } 38 };

【版權聲明】轉載請注明出處:http://www.cnblogs.com/TenosDoIt/p/3440059.html

轉載于:https://www.cnblogs.com/TenosDoIt/p/3440059.html

總結

以上是生活随笔為你收集整理的LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 免费看日批 | 精品久久久久久久久久久久久久久 | 国产精品丝袜视频无码一区69 | 亚洲jlzzjizz少妇 | 一区二区福利 | 免费福利视频网站 | 欧美色亚洲色 | 被绑在床强摁做开腿呻吟 | 乱人伦av| 在线视频欧美亚洲 | 伊人久久在线 | 精品国产自 | 日日夜夜精品 | 中文字幕在线免费看 | 污污视频在线免费看 | 亚洲大胆 | 欧美暧暧视频 | 亚洲精品www久久久久久广东 | 亚洲天堂2015 | 自拍视频一区 | 久久久久人妻一区精品色 | 久色福利 | 国产福利在线视频 | 久久国产精品视频 | 国产一区二区视频在线观看免费 | 欧美福利在线 | 91麻豆成人 | 日韩欧美一区二区区 | 九九色视频 | 幸福宝在线观看 | 中文字幕乱码在线 | 老熟妇仑乱视频一区二区 | 欧美黄色一级生活片 | 91av福利| 国产传媒视频在线观看 | 国产草草浮力影院 | 亚洲综合激情 | 2018天天干天天操 | 免费看裸体视频网站 | 国产成人久久精品 | 国产性猛交xxxx免费看久久 | 中文字字幕在线中文乱码电影 | 91性高潮久久久久久久久 | 宗合久久| 亚洲国产精品99久久 | 日本一本久 | 激情亚洲 | 97在线观看免费 | 亚洲特黄特色 | 欧美日韩一区在线播放 | 亚洲精品aⅴ | 黑丝少妇喷水 | 91视频精品 | 人人妻人人爽一区二区三区 | 久久成人久久 | 99re8在线精品视频免费播放 | 国产手机视频在线 | 玖玖玖在线观看 | 今天高清视频在线观看视频 | 国产又粗又猛又爽69xx | 国产在线视频资源 | 秋霞电影网一区二区 | 欧美在线一级 | 久久密 | 国产高潮视频在线观看 | 国产高清视频在线观看 | 北条麻妃一区二区三区在线观看 | 色爱AV综合网国产精品 | 日韩在线视频播放 | 精品一卡二卡三卡 | 一区二区三区在线观看 | 1级av| 成人激情视频在线播放 | 日本精品免费在线观看 | 日韩av高清在线播放 | 自拍亚洲综合 | 美女视频黄色 | 欧美色综合网 | 在线观看免费 | 国产对白视频 | 看了让人下面流水的视频 | 久久精品资源 | 久久精品国产亚洲av香蕉 | 免费福利在线视频 | 天天草天天操 | 91亚洲国产成人久久精品麻豆 | 女人18毛片一区二区三区 | 中文字幕精品一区久久久久 | 日韩亚洲欧美在线观看 | 神马午夜国产 | 国产一区二区三区四区五区六区 | 东京热无码av一区二区 | 中文字幕 成人 | 国产一级片免费在线观看 | 亚洲av无码专区在线电影 | 国产免费黄色小视频 | 欧美人与动物xxxx | 国产精品久久亚洲 | 日本美女一级视频 |