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 countsub(TreeNode *root) {
13 int minsub=1;
14 if ((root->left != NULL) && (root->right != NULL))
15 {
16 int temp1 = countsub(root->left) ;
17 int temp2 = countsub(root->right) ;
18 minsub += (temp1> temp2) ? temp1 : temp2;
19 }
20 else if ((root->left != NULL))
21 {
22 minsub += countsub(root->left) ;
23 }
24 else if ((root->right != NULL))
25 {
26 minsub += countsub(root->right) ;
27 }
28
29 return minsub;
30 }
31 int maxDepth(TreeNode *root) {
32 // Start typing your C/C++ solution below
33 // DO NOT write int main() function
34 int min = 0;
35 if (root == NULL)
36 {
37 min = 0;
38 }
39 else{
40 min =1;
41 if ((root->left != NULL) && (root->right != NULL))
42 {
43 int temp1 = countsub(root->left) ;
44 int temp2 = countsub(root->right) ;
45 min += (temp1> temp2) ? temp1 : temp2;
46 }
47 else if ((root->left != NULL))
48 {
49 min += countsub(root->left) ;
50 }
51 else if ((root->right != NULL))
52 {
53 min += countsub(root->right) ;
54 }
55 }
56 return min;
57 }
58
59 };
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int countsub(TreeNode *root) {
int minsub=1;
if ((root->left != NULL) && (root->right != NULL))
{
int temp1 = countsub(root->left) ;
int temp2 = countsub(root->right) ;
minsub += (temp1> temp2) ? temp1 : temp2;
}
else if ((root->left != NULL))
{
minsub += countsub(root->left) ;
}
else if ((root->right != NULL))
{
minsub += countsub(root->right) ;
}
return minsub;
}
int maxDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int min = 0;
if (root == NULL)
{
min = 0;
}
else{
min =1;
if ((root->left != NULL) && (root->right != NULL))
{
int temp1 = countsub(root->left) ;
int temp2 = countsub(root->right) ;
min += (temp1> temp2) ? temp1 : temp2;
}
else if ((root->left != NULL))
{
min += countsub(root->left) ;
}
else if ((root->right != NULL))
{
min += countsub(root->right) ;
}
}
return min;
}
};
原文链接: https://www.cnblogs.com/chunlifang-luck/archive/2013/05/11/3073428.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/88077
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!