Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

111. 二叉树的最小深度 #92

Open
Labels
@Geekhyt

Description

@Geekhyt

原题链接

递归 dfs

  1. root 为空时,高度为 0
  2. root 的左右子树都为空时,高度为 1
  3. 如果左子树或者右子树为空时,返回另一棵子树的高度
  4. 否则返回两棵子树的高度最小值
constminDepth=function(root){if(root===null)return0if(root.left===null&&root.right===null){return1}if(root.left===null){return1+minDepth(root.right)}if(root.right===null){return1+minDepth(root.left)}returnMath.min(minDepth(root.left),minDepth(root.right))+1}
  • 时间复杂度:O(n)
  • 空间复杂度:O(logn)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp