<1> Max Depth of Binary Tree
左右两边的哪边最大就跟哪边。
int maxDepth(TreeNode *root) {
if(root == NULL) return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
<2> Min Depth of Binary Tree
左右两边哪边最小就跟哪边。但是!如果有一边没有了,还是要加上另外一边,因为要找到叶子节点为止。
<gs id="881de3b1-2f56-4007-8a93-24f720699e47" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">int</gs> <gs id="541202fc-7628-4d17-b616-2e7be1777106" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">minDepth</gs><gs id="efeb868a-b551-4548-a855-e37c9856fc90" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">(</gs>TreeNode *root<gs id="30841f59-d3aa-46f7-924d-9a5e2792fde1" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">)</gs><gs id="0cfc2fb7-799a-463c-944f-b3cdfe059579" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">{
</gs>if<gs id="65159b39-94c9-403d-b7ef-dd4e97c83374" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">(</gs>root == NULL) return 0;
if<gs id="a16bc1ad-155f-44e4-ac8f-6e3f966f4cb2" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">(</gs>root->left == NULL) return <gs id="ac70b0c0-5687-43e7-a2fb-e30d915c7064" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">minDepth</gs><gs id="4c2c084a-2d47-4f2e-b09b-b069554ea8c6" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">(</gs>root->right) + 1;
if<gs id="e6a400f6-3335-4c3a-a748-8f5b419c2f7f" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">(</gs>root->right == NULL) return <gs id="df37c83e-a8b6-4799-95c9-b30d7df1b22d" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">minDepth</gs><gs id="f070016c-22a9-45d2-84ed-ef5a271060e4" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">(</gs>root->left) + 1;
return min<gs id="e7ea1585-4dca-4e0e-b3ea-44f7fd3b40f7" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">(</gs><gs id="1be10de8-d6bc-4b2b-90c9-eaa38a44e905" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">minDepth</gs><gs id="9bd58580-360f-47f3-a572-fa22808e88d9" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">(</gs>root->left)<gs id="31ce111d-9b0f-4c75-a702-5df73fc8dd72" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">,</gs><gs id="8c14ffd5-d003-45ac-85f8-4f0fe6ce4fa0" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">minDepth</gs><gs id="cf499b39-116a-4ac6-9bf0-eb1dd032553c" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">(</gs>root->right)) + 1;<gs id="819215eb-ca0c-4462-8804-9c964e8b8777" ginger_software_uiphraseguid="555b21d2-2fa2-4e68-b619-290987d02f4c" class="GINGER_SOFTWARE_mark">
}</gs>