题目描述
给定一个二叉树 root ,返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
出处
思路
递归遍历即可。
代码
class Solution {
private:
void acc_h(TreeNode* root,int t,int& h){
if(!root) return;
t++;
if(t>h)
h=t;
if(root->left) acc_h(root->left,t,h);
if(root->right) acc_h(root->right,t,h);
}
public:
int maxDepth(TreeNode* root) {
int t=0,h=0;
acc_h(root,t,h);
return h;
}
};