problem

111. Minimum Depth of Binary Tree

 code

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root) return 0;
if(!root->left) return 1+minDepth(root->right);
if(!root->right) return 1+minDepth(root->left);
return 1+min(minDepth(root->left), minDepth(root->right));

}
};

 

 

 

 

参考

​1. Minimum Depth of Binary Tree​​;