叭讲了,这个就是一个对递归遍历。背一背吧,学树必学。
/**
* 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) {
if(!root) return 0;
int a_left=maxDepth(root->left);
int a_right=maxDepth(root->right);
return max(a_left,a_right)+1;
}
};