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

来源:互联网 发布:淘宝网名龙堂 编辑:程序博客网 时间:2024/06/05 10:09

*****************************我是分割线************************
/**
* 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);
}
}
};

这个还没有研究特别清楚啦,对树的操作还需要练习,(^__^) 嘻嘻……

1 0
原创粉丝点击