题解 | #二叉树的最小深度#
二叉树的最小深度
https://www.nowcoder.com/practice/e08819cfdeb34985a8de9c4e6562e724
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @return int整型 */ int run(TreeNode* root) { if(root==nullptr){ return 0; } if(root->left==nullptr&&root->right==nullptr){ return 1; } int l=run(root->left); int r=run(root->right); int ans; if(l==0){ ans=r; } else if(r==0){ ans=l; } else{ ans=l<r?l:r; } return ans+1; } };