maximum depth

 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;
    }

};

 

posted @ 2013-05-11 21:30  立春了  Views(181)  Comments(0Edit  收藏  举报