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) {
if(root == NULL)
return 0;
if(root->left == NULL && root->right == NULL)
return 1;
int max_left = 0;
int max_right = 0;
max_left = maxDepth(root->left) + 1;
max_right = maxDepth(root->right) + 1;
return max_left > max_right ? max_left : max_right;
}
目测还有效率更高的算法,还在思考