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

112. 路径总和 #77

Open
Open
Labels
@Geekhyt

Description

@Geekhyt

原题链接

递归

  1. 处理边界,节点不存在时返回 false
  2. 左右子树都不存在时代表是叶子结点,判断是否符合条件
  3. 递归左右子树时进行转换,看能否找到targetSum - root.val 的路径
consthasPathSum=(root,targetSum)=>{if(root===null)returnfalseif(root.left===null&&root.right===null){returntargetSum-root.val===0}returnhasPathSum(root.left,targetSum-root.val)||hasPathSum(root.right,targetSum-root.val)}
  • 时间复杂度: O(n)
  • 空间复杂度: O(H),H 是树的高度

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