zoukankan      html  css  js  c++  java
  • LeetCode429. N-ary Tree Level Order Traversal

    题目来源:429. N-ary Tree Level Order Traversal

    https://leetcode.com/problems/n-ary-tree-level-order-traversal/

     
    自我感觉难度/真实难度:hard/easy

    队列操作不熟悉

    题意:

     层序遍历树

    分析:
     
    自己的代码:
    """
    # Definition for a Node.
    class Node(object):
        def __init__(self, val, children):
            self.val = val
            self.children = children
    """
    class Solution(object):
        def levelOrder(self, root):
            """
            :type root: Node
            :rtype: List[List[int]]
            """
            if not root:
                return []
            res=[]
            temp=[]
           
            for i,j in enumerate(root):
                res.append([i])
                temp.push(j)
            levelOrder(temp)
            return res
                
                
                
            
    代码效率/结果:
     
    优秀代码:
    """
    # Definition for a Node.
    class Node(object):
        def __init__(self, val, children):
            self.val = val
            self.children = children
    """
    class Solution(object):
        def levelOrder(self, root):
            """
            :type root: Node
            :rtype: List[List[int]]
            """
            res = []
            que = collections.deque()
            que.append(root)
            while que:
                level = []
                size = len(que)
                for _ in range(size):
                    node = que.popleft()
                    if not node:
                        continue
                    level.append(node.val)
                    for child in node.children:
                        que.append(child)
                if level:
                    res.append(level)
            return res
    代码效率/结果:
     
    自己优化后的代码:
     
    """
    # Definition for a Node.
    class Node(object):
        def __init__(self, val, children):
            self.val = val
            self.children = children
    """
    class Solution(object):
        def levelOrder(self, root):
            """
            :type root: Node
            :rtype: List[List[int]]
            """
            if not root:
                return []
            res=[]
            
            que=collections.deque()
            que.append(root)
            while que:
                temp=[]                 #每次要使用的临时变量
                
                size=len(que)           #队列的循环通过size来实现
                for _ in range(size):
                    node=que.pop()
                    if not node:
                        continue
                    temp.append(node.val)
                    for child in node.children:
                        que.append(child)
                
                if temp:
                    res.append(temp)
            
            return res
    反思改进策略: 

       1.对队列的操作不熟悉

        

         que=collections.deque()  #队列的构造
            while que:
                temp=[]                 #每次要使用的临时变量
                
                size=len(que)           #队列的循环通过size来实现
                for _ in range(size):
                    node=que.pop()      #通过弹出前面的元素来实现,    队列长这个样子:que(),里面放一个长的list

      2.

  • 相关阅读:
    aa
    https php
    字节转换(转载)
    mysql和mariadb中字段拼接类型有两个或多个字段或者一个字段和一个固定字符串拼接
    Qt检测显示器的个数
    QWebEngine自动添加麦克风和摄像头权限
    Qt开启和关闭蓝牙
    cef关闭主窗口并调用CefQuitMessageLoop()后,仍然在后台运行的问题(linux)
    windows安装jq
    Allure2环境准备
  • 原文地址:https://www.cnblogs.com/captain-dl/p/10176342.html
Copyright © 2011-2022 走看看