Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Geekhyt opened this issue Sep 20, 2021 · 0 comments
Open

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

Geekhyt opened this issue Sep 20, 2021 · 0 comments
Labels

Comments

@Geekhyt
Copy link
Owner

Geekhyt commented Sep 20, 2021

原题链接

递归 dfs

  1. root 为空时,高度为 0
  2. root 的左右子树都为空时,高度为 1
  3. 如果左子树或者右子树为空时,返回另一棵子树的高度
  4. 否则返回两棵子树的高度最小值
const minDepth = function(root) {
    if (root === null) return 0
    if (root.left === null && root.right === null) {
        return 1
    }
    if (root.left === null) {
        return 1 + minDepth(root.right)
    }
    if (root.right === null) {
        return 1 + minDepth(root.left)
    }
    return Math.min(minDepth(root.left), minDepth(root.right)) + 1
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(logn)
@Geekhyt Geekhyt added the 简单 label Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant