题解 | #二叉树的最小深度#
二叉树的最小深度
https://www.nowcoder.com/practice/6a7f40d7696d46f79c74c61179993be6
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: int run(TreeNode* root) { // write code here if(root==nullptr) return 0; int l = run(root->left)+1; int r = run(root->right)+1; if(l>1&&r>1) return min(l, r); else return max(l,r); } };