题解 | #牛群排列的最大深度#
牛群排列的最大深度
https://www.nowcoder.com/practice/b3c6383859a142e9a10ab740d8baed88
1.考察知识点:
二叉树遍历、递归
2.编程语言:
C
3.解题思路:
直接采用递归,返回左子树深度+右子树深度+1(表示根节点深度)即可
4.完整代码:
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型
*/
int max(int a,int b)
{
return a > b ? a : b;
}
int maxDepth(struct TreeNode* root ) {
// write code here
if(root == NULL) return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
#面试高频TOP202#
基恩士成长空间 453人发布