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.
题目获取一个二叉树的最大深度:
首先想到的当然是DFS算法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode* root) {
if (root==NULL)
return 0;
int suml=maxDepth(root->left);
int sumr=maxDepth(root->right);
return suml>sumr?suml+1:sumr+1;
}