# int32bit / leetcode

Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
solve.cpp

## Minimum Depth of Binary Tree

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.

## Solution

• 最小深度的概念没有弄清楚，必须是到叶子节点中最短的路径
``````  1
/
3
``````

• 利用宏求最小值，重复计算导致超时
``````#define MIN(a, b) ((a) < (b) : (a) : (b))

MIN(minDepth(root->left), minDepth(root->right));

minDepth(root->left) < minDepth(root->right) ? minDepth(root->left) : minDepth(root->right);
``````

** 因此以后尽量不要使用宏定义， 而尽量使用内联函数 **

# Code

```int minDepth(struct TreeNode *root) {
if (root == NULL)
return 0;
if (root->left && root->right)
return min(minDepth(root->left), minDepth(root->right)) + 1;
else if (root->left)
return minDepth(root->left) + 1;
else
return minDepth(root->right) + 1;
}```

## 扩展

You can’t perform that action at this time.