https://leetcode.cn/problems/er-cha-shu-de-shen-du-lcof/description/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int calculateDepth(TreeNode* root)
{
// 采用一种不借助全局变量记录深度的递归方法
if(root == nullptr)
{
return 0;
}
int left_depth = calculateDepth(root->left); // 定义为int型,返回每一次递归的深度
int right_depth = calculateDepth(root->right);
return max(left_depth,right_depth) + 1;
}
};