计算二叉树的深度

计算二叉树深度:leetCode

1. 递归调用

/**
 * 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 maxDepth(TreeNode *root) {
        if(root==NULL)
        return 0;
        if (root->left!=NULL&&root->right!=NULL)
           return maxDepth(root->left)>maxDepth(root->right)?maxDepth(root->left)+1:maxDepth(root->right)+1;//超时
        else if(root->left==NULL&&root->right!=NULL)
           return 1+maxDepth(root->right);
        else if(root->left!=NULL&&root->right==NULL)
           return 1+maxDepth(root->left);
        else if(root->left==NULL&&root->right==NULL)
           return 1;
    }
};

最后,超时。发现在有注释的行,在前面比较时多次递归,在比较完后赋值时又递归,所以超时。

修改为下面的代码:

 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         if(root==NULL)
14         return 0;
15         else if (root->left!=NULL&&root->right!=NULL)
16         {
17             int ld=maxDepth(root->left);
18             int rd=maxDepth(root->right);
19             return ld>rd?ld+1:rd+1;
20         }
21            
22         else if(root->left==NULL&&root->right!=NULL)
23            return 1+maxDepth(root->right);
24         else if(root->left!=NULL&&root->right==NULL)
25            return 1+maxDepth(root->left);
26         else if(root->left==NULL&&root->right==NULL)
27            return 1;
28     }
29 };
View Code

AC了。

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         if(root==NULL)
14         return 0;
15         int ldepth=maxDepth(root->left);
16         int rdepth=maxDepth(root->right);
17         return ldepth>rdepth?ldepth+1:rdepth+1;
18     }
19 };
View Code

3. 代码还可以采用以下方式,不过行数比较多,跟第二种一样

 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,int *depth)
13     {
14         if(root==NULL)
15           return *depth=0;
16         int ldepth,rdepth;
17         ldepth=maxDepth(root->left,&ldepth)+1;
18         rdepth=maxDepth(root->right,&rdepth)+1;
19         return ldepth>rdepth?ldepth:rdepth;
20     }
21     int maxDepth(TreeNode *root) {
22         int depth=0;
23         return maxDepth(root, &depth);
24     }
25 };
View Code

这道题目不涉及一个节点多次计算的问题,当比较二叉树是否是平衡树时,可能会产生,用第三中方法能有效避免。参考《剑指offer》209页。

 

 

 

 

posted @ 2013-12-14 13:31  蓝兔子  阅读(697)  评论(0编辑  收藏  举报