BM28 二叉树的最大深度
给定一个二叉树的根节点root,返回它的后序遍历结果。
解法
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型
*/
typedef struct TreeNode* pnode ;
void max_depth(pnode root , int now_depth , int* max_depths )
{
if(!root)
{
*max_depths = now_depth>*max_depths?now_depth:*max_depths;
return;
}
max_depth(root->left , now_depth+1 , max_depths);
max_depth(root->right , now_depth+1 , max_depths);
}
int maxDepth(struct TreeNode* root ) {
// write code here
int max_depths = 0;
max_depth(root , 0 , &max_depths);
return max_depths;
}