思路
DFS,二叉树的后序遍历,先求左子树最大深度,再求右子树最大深度,左右子树最大深度加一即为二叉树最大深度。
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};