zoukankan      html  css  js  c++  java
  • leetcode_107. 二叉树的层次遍历 II

    给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
    
    例如:
    给定二叉树 [3,9,20,null,null,15,7],
    
        3
       / 
      9  20
        /  
       15   7
    返回其自底向上的层次遍历为:
    
    [
      [15,7],
      [9,20],
      [3]
    ]
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
            ls=[]
            que=[root]
            if not root:return ls
            while que:
                t=[]
                length=len(que)
                for _ in range(length):
                    temp=que.pop(0)
                    t.append(temp.val)
                    if temp.left :
                        que.append(temp.left)
                    if temp.right:
                        que.append(temp.right)
                ls.append(t)
            return ls[::-1]
    
  • 相关阅读:
    css3
    如何去渲染数据?
    ajax
    Java多线程-线程安全
    java多线程-基础
    Git-团队开放中的代码同步与提交
    IDEA 调试Spring-boot 应用
    微服务-各种pom的配置和注解
    微服务-服务与注册中心
    微服务
  • 原文地址:https://www.cnblogs.com/hqzxwm/p/14048820.html
Copyright © 2011-2022 走看看