Minimum Depth of Binary Tree [LeetCode]

时间:2021-10-11 04:10:46

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.

Summary: BFS or DFS with recursion.

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