/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*///dfsintmaxDepth(structTreeNode* root){int max =0;if(NULL==root){return0;}if(NULL==root->left&&NULL==root->right){//是叶子结点return1;}if(root->left){//有左子树int n =1+maxDepth(root->left);
max = max>n?max:n;//下面一行等价于上面两行,便于理解//max = max>1+maxDepth(root->left)?max:1+maxDepth(root->left);//只取遍历结果的最大值,也即左右子树的max共同比较}if(root->right){//有右子树int n =1+maxDepth(root->right);
max = max>n?max:n;}return max;}