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

429. N 叉树的层序遍历 #89

Open
Labels
@Geekhyt

Description

@Geekhyt

原题链接

队列 bfs

使用队列进行广度优先遍历,level 数组存放当前层级的值,处理完一层后推入结果数组 result。

constlevelOrder=function(root){if(root==null)return[]constresult=[]constqueue=[root]while(queue.length>0){constlevelSize=queue.lengthconstlevel=[]for(leti=0;i<levelSize;i++){consthead=queue.shift()for(constchildofhead.children){queue.push(child)}level.push(head.val)}result.push(level)}returnresult}
  • 时间复杂度:O(n)
  • 空间复杂度:O(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