首页 > 其他分享 >leetcode 107. Binary Tree Level Order Traversal II

leetcode 107. Binary Tree Level Order Traversal II

时间:2023-05-30 17:36:57浏览次数:43  
标签:node Binary return Level self Tree ans paths root

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

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

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root: 
            return []
        q = [root]
        ans = []
        while q:
            ans.append([n.val for n in q])
            q = [n for node in q for n in (node.left, node.right) if n]           
        return ans[::-1]

上面解法是层序遍历,使用先序遍历,递归:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """        
        def dfs(node, paths, depth):
            if not node: return
            if len(paths) == depth:
                paths.append([])                
            paths[depth].append(node.val)
            dfs(node.left, paths, depth+1)
            dfs(node.right, paths, depth+1)
        ans = []
        dfs(root, ans, 0)
        return ans[::-1]

 

标签:node,Binary,return,Level,self,Tree,ans,paths,root
From: https://blog.51cto.com/u_11908275/6380981

相关文章

  • leetcode 101. Symmetric Tree
    Givenabinarytree,checkwhetheritisamirrorofitself(ie,symmetricarounditscenter).Forexample,thisbinarytree[1,2,2,3,4,4,3]issymmetric:1/\22/\/\3443Butthefollowing[1,2,2,null,3,null,3]isnot:1/\2......
  • java treemap
    TreeMap是Java中的一个类,它实现了Map接口,利用红黑树数据结构来有序存储键值对。TreeMap中的键按升序排序,若要自定义排序方式,则可以提供自定义的比较器。TreeMap实现了高效的数据访问、插入和删除操作,大多数常规操作的时间复杂度为O(logn)。importjava.util.TreeMap;public......
  • can't not find Node.js binary ''path",make sure Node.js is installed and in your
    vscode中node执行debug报错报错信息如下 思路1:检查node是否安装成功win+R输入cmd  能够正常显示版本号,则证明没有问题,接着换个思路 思路2:根据提示打开图示的'launch.json'文件,在页面补充 runtimeExecutable具体补充什么内容呢?在overflow中找到了nginx中需要补......
  • SourceTree使用教程
    SourceTree使用教程1.克隆、提交、推送​ 在使用SourceTree之前必须要先安装Git和sourceTree,具体安装过程不再赘述(1)以加入我的管理团队为例,进入5-27-dq这个仓库,点击管理,然后进入仓库成员管理,发现现在我的仓库成员有4个了,gitee免费版最多可5个成员。​ 若要加入我的代码仓,请......
  • 算法刷题记录:珂朵莉的假toptree
    题目链接https://ac.nowcoder.com/acm/contest/19306/1035题目分析将每个数每一位都进行拆分即可。AC代码#include<iostream>usingnamespacestd;intn,p=1,num=1;inta[1005];intmain(){cin>>n;while(p<=1000){if(num>=1......
  • 1151 LCA in a Binary Tree
    题目:Thelowestcommonancestor(LCA)oftwonodesUandVinatreeisthedeepestnodethathasbothUandVasdescendants.Givenanytwonodesinabinarytree,youaresupposedtofindtheirLCA.InputSpecification:Eachinputfilecontainsonetest......
  • 【技术分享】万字长文图文并茂读懂高性能无锁 “B-Tree 改”:Bw-Tree
    【技术分享】万字长文图文并茂读懂高性能无锁“B-Tree改”:Bw-Tree原文链接:https://mp.weixin.qq.com/s/I5TphQP__tHn6JoPcP--_w参考文献不一定能下载。如果你获取不到这几篇论文,可以关注公众号IT技术小密圈回复bw-tree获取。一.背景Bw-Tree希望实现以下能力:解决......
  • 力扣 662 https://leetcode.cn/problems/maximum-width-of-binary-tree/
    需要了解树的顺序存储如果是普通的二叉树,底层是用链表去连接的如果是满二叉树,底层用的是数组去放的,而数组放的时候会有索引对应当前父节点是索引i,下一个左右节点就是2i,2i+1利用满二叉树的索引特征所以需要对每个节点进行一个索引赋值,赋值在队列中,队列用数组表示核心代码......
  • Paper Reading: Adaptive Neural Trees
    目录研究动机文章贡献自适应神经树模型拓扑与操作概率模型与推理优化实验结果模型性能消融实验可解释性细化阶段的影响自适应模型复杂度优点和创新点PaperReading是从个人角度进行的一些总结分享,受到个人关注点的侧重和实力所限,可能有理解不到位的地方。具体的细节还需要以原文......
  • Cassandra中的MerkleTree反熵机制
    构建MerkleTreeCassandra是一个分布式数据库系统,它使用Merkle树来实现数据一致性和数据完整性的验证。在Cassandra中,每个节点都维护着自己的数据副本。为了确保数据的一致性和完整性,Cassandra使用Merkle树进行验证。Merkle树是一种树状结构,由哈希值构成,用于对数据块进......