GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode102:二叉树的层序遍历 · Issue #47 · sisterAn/JavaScript-Algorithms

leetcode102:二叉树的层序遍历 #47

New issue
Copy link
New issue
Copy link
Open
Open
leetcode102:二叉树的层序遍历#47
Copy link
Labels
LeetCode剑指Offer字节

Description

@sisterAn
sisterAn
opened on May 21, 2020
Issue body actions

给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。 

示例:
二叉树:[3,9,20,null,null,15,7] ,

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

Activity

  1. sisterAn
    added
    LeetCode
    字节
    剑指Offer
    on May 21, 2020
  2. plane-hjh commented on May 21, 2020

    @plane-hjh
    plane-hjh
    on May 21, 2020
    More actions

    这道题和二叉树的层次遍历相似,只需要把 reverse() 去除就可以了

    广度优先遍历

    var levelOrder = function(root) {
        if(!root) return []
        let res = [], 
            queue = [root]
        while(queue.length) {
            let curr = [],
                temp = []
            while(queue.length) {
                let node = queue.shift()
                curr.push(node.val)
                if(node.left) temp.push(node.left)
                if(node.right) temp.push(node.right)
            }
            res.push(curr)
            queue = temp
        }
        return res
    };

    深度优先遍历

    var levelOrder = function(root) {
        const res = []
        var dep = function (node, depth){
            if(!node) return
            res[depth] = res[depth]||[]
            res[depth].push(node.val)
            dep(node.left, depth + 1)
            dep(node.right, depth + 1)
        }
        dep(root, 0)
        return res
    };
  3. AnranS commented on Nov 19, 2020

    @AnranS
    AnranS
    on Nov 19, 2020
    More actions
    var levelOrder = function(root) {
        let res = [], queue = [];
        if(!root) return res;
        queue.push(root);
    
        while(queue.length) {
            let len = queue.length;
            res.push([]);
            for(let i = 0;i<len;i++) {
                let node = queue.shift();
                res[res.length - 1].push(node.val);
                if(node.left) queue.push(node.left);
                if(node.right) queue.push(node.right);
            }
        }
        return res;
    };
  4. lewisYe commented on Apr 9, 2021

    @lewisYe
    lewisYe
    on Apr 9, 2021
    More actions

    使用广度优先遍历 然后再对数据进行分层

    var levelOrder = function(root) {
        if(!root || root.val == null) return []
       let queue = []
       let res = []
       queue.push(root)
       while(queue.length){
           let len = queue.length
           let curr = []
           for(var i =0;i<len;i++){
               let node = queue.shift()
               curr.push(node.val)
                if(node.left)queue.push(node.left)
                if(node.right)queue.push(node.right)
           }
           res.push(curr)
       }
        return res
    };
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    LeetCode剑指Offer字节

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.