zoukankan      html  css  js  c++  java
  • Leetcode 107二叉树遍历,

    107.二叉树的遍历

    给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

    例如:
    给定二叉树 [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]]:
    l = []
    if not root:
    return l
    tmp = [root]
    while tmp:
    tmp_l = []
    tmp_bianli = []
    for item in tmp:
    tmp_l.append(item.val)
    if item.left:
    tmp_bianli.append(item.left)
    if item.right:
    tmp_bianli.append(item.right)
    l.append(tmp_l)
    tmp = tmp_bianli
    return l[::-1]

  • 相关阅读:
    负外边距--转载
    研究Dropbox Server端文件系统
    Bluetooth Profile for iPhone from the functional perspectives
    Somebody That I Used to Know
    复合查询
    聚合查询
    Filter查询
    ES基本查询
    ES版本控制
    ES基本操作
  • 原文地址:https://www.cnblogs.com/xqy-yz/p/11411160.html
Copyright © 2011-2022 走看看