递归解法:
/**
* 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 max = 0;
int leftDepth = 0;
int deep1 = maxDepth(root->left);
int deep2 = maxDepth(root->right);
return deep1 > deep2? deep1+1:deep2+1;
}
};
点击打开链接