麻,递归就完事,当前节点的树高等于左右子树中大的那个树高+1
代码如下所示:
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
int maxDepth(TreeNode* root) {
// write code here
if(root==NULL) return 0;
int h_left=maxDepth(root->left);
int h_right=maxDepth(root->right);
int h=max(h_left, h_right) + 1;
return h;
}
};