依然使用递归思想。
思路:
一、树的深度 = max (左子树深度,右子树深度)+ 1 。 ------> 这里的加1是表示本身节点深度为1。
二、若是当前节点为null,则说明它的左右子树深度为0。code
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; }