Min Depth of Binary Tree Time : Space : 123456789101112131415161718192021/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */void dfs(TreeNode* node, int dep, int& res) { if(!node->left and !node->right) res = min(res,dep); if(node->left) dfs(node->left, dep + 1, res); if(node->right) dfs(node->right, dep + 1, res);}int Solution::minDepth(TreeNode* A) { if(!A) return 0; int res = INT_MAX; dfs(A,1,res); return res;}