依然使用递归思想。
思路:
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; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/602839.html