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

100. 相同的树 #16

Open
Open
Labels
@Geekhyt

Description

@Geekhyt

原题链接

深度优先搜索 DFS

1.如果两个二叉树都为空,则它们相同返回 true。
2.如果两个二叉树中有一个为空,则它们不同返回 false。
3.如果两个二叉树都不为空,首先判断根节点是否相同,不同则返回 false。
4.如果两个二叉树的根节点相同,则分别递归判断其左右子树是否相同。

constisSameTree=function(p,q){if(p===null&&q===null)returntrue;if(p===null||q===null)returnfalse;if(p.val!==q.val)returnfalse;returnisSameTree(p.left,q.left)&&isSameTree(p.right,q.right);};
  • 时间复杂度: O(min(m, n))
  • 空间复杂度: O(min(m, n))

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