0%

【树】最小数目深度

题目描述

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.

解题思路

1
2
3
4
5
6
7
8
9
10
11
12
13
int run(TreeNode *root) {
if(root==nullptr) return 0;
if(root->left==nullptr&&root->right==nullptr)
return 1;
if(root->left==nullptr&&root->right!=nullptr)
return run(root->right)+1;
if(root->left!=nullptr&&root->right==nullptr)
return run(root->left)+1;
int l=1,r=1;
l+=run(root->left);
r+=run(root->right);
return l<r?l:r;
}