leetcode之104. Maximum Depth of Binary Tree(C++解法)

*****************************我是分割線************************
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
int num;
if(root==NULL)
return 0;
else if(root->left==0 && root->right==0)
return 1;
else if(root->left==0)
return 1+maxDepth(root->right);
else if(root->right==0)
return 1+maxDepth(root->left);
else
{
int a=1+maxDepth(root->left);
int b=1+maxDepth(root->right);
return max(a,b);
}
}
};

這個還沒有研究特別清楚啦,對樹的操作還需要練習,(^__^) 嘻嘻……

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章