美文网首页leetcode --- js版本程序员
leetcode-tree-111- Minimum Depth

leetcode-tree-111- Minimum Depth

作者: 石头说钱 | 来源:发表于2019-06-13 23:14 被阅读0次

题目:Balanced Binary Tree

Minimum Depth of Binary Tree

  • Example 1
    Given the following tree [3,9,20,null,null,15,7]:
    3
   / \
  9  20
    /  \
   15   7

minimum depth = 2

解法

function minimumDepth(root){
    if(!root) return 0;
    if(!root.left) return 1 + minimumDepth(root.right)
    if(!root.right) return 1 + minimumDepth(root.left)
    return 1+ Math.min(minimumDepth(root.left,root.right))
}

相关文章

网友评论

    本文标题:leetcode-tree-111- Minimum Depth

    本文链接:https://www.haomeiwen.com/subject/kehyfctx.html