解题思路: 递归遍历二叉树,每次调用返回左右节点高度的最大值。
通关代码:
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == NULL) {
return 0;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return max(left, right) + 1;
}
};
通关截图: