【LeetCode 0111】【递归/分治】二叉树的最小深度

07-14 1025阅读

  1. 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.

Note: A leaf is a node with no children.

Example 1:

【LeetCode 0111】【递归/分治】二叉树的最小深度

**Input:** root = [3,9,20,null,null,15,7]
**Output:** 2

Example 2:

**Input:** root = [2,null,3,null,4,null,5,null,6]
**Output:** 5

Constraints:

  • The number of nodes in the tree is in the range [0, 10^5].
  • -1000 if(!root){ return 0 } if(!root.left ){ return minDepth(root.right) + 1 } if( !root.right){ return minDepth(root.left) + 1 } return Math.min(minDepth(root.left),minDepth(root.right)) + 1 };
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]