Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
题意:求二叉树的最大深度(根节点到叶节点的节点数)
对于树,一般用递归比较简单,代码明确,先试试递归吧!
int maxDepth(TreeNode *root) {
int dep = 0;
if(root != NULL){
int leftDep = maxDepth(root->left);
int rightDep = maxDepth(root->right);
dep = leftDep > rightDep ? leftDep : rightDep;
dep ++;
}
return dep;
}