依然使用递归思想。
思路:
1、树的深度 = max (左子树深度,右子树深度)+ 1 。 ——> 这里的加1是表示自己节点深度为1。
2、如果当前节点为null,则说明它的左右子树深度为0。
int max(int a, int b)
{
if (a>b)
return a;
else
return b;
}
int maxDepth(struct TreeNode* root){
int iDepth = 0;
if (NULL == root)
return 0;
iDepth = max(maxDepth(root->left), maxDepth(root->right)) + 1;
return iDepth;
}
Original: https://www.cnblogs.com/payapa/p/11117059.html
Author: 努力爬呀爬
Title: leetcode的Hot100系列–104. 二叉树的最大深度
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/565249/
转载文章受原作者版权保护。转载请注明原作者出处!