LeetCode Minimum Depth of Binary Tree, Oct 10 '121893 / 4635
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
思路:递归
- int minDepth(TreeNode *root) {
- if(!root) return 0;
- if(!root->left && !root->right) return 1;
- int leftmin = INT_MAX, rightmin = INT_MAX;
- if(root->left){
- leftmin = minDepth(root->left);
- }
- if(root->right){
- rightmin = minDepth(root->right);
- }
- return min(leftmin, rightmin) + 1;
- }
No comments:
Post a Comment