Maximum Depth of Binary Tree

int maxDepth(TreeNode* root) {

if(!root) return 0;

if(!root->left)//必须是leaf

return maxDepth(root->right)+1;

if(!root->right)

return maxDepth(root->left)+1;

return 1+max(maxDepth(root->left),maxDepth(root->right));

}

Last updated

Was this helpful?