Minimum Depth of Binary Tree

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 
13     int min=10000;
14     void meng(TreeNode*root,int dep)
15     {
16         if((root->left==NULL)&&(root->right==NULL))
17         {
18             if(dep<min)
19                 min=dep;
20             return;
21         }
22         if(root->left)
23             meng(root->left,dep+1);
24         if(root->right)
25             meng(root->right,dep+1);
26     }
27     int minDepth(TreeNode *root) {
28         // Start typing your C/C++ solution below
29         // DO NOT write int main() function
30         min=10000;
31         if(root==NULL)
32             return 0;
33         meng(root,1);
34         return min;
35     }
36 };

原文链接: https://www.cnblogs.com/mengqingzhong/archive/2013/05/11/3072975.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/88051

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午11:24
下一篇 2023年2月9日 下午11:24

相关推荐