LeetCode题解(1137):二叉树中的列表(Python)
发布日期:2021-06-29 19:56:49 浏览次数:2 分类:技术文章

本文共 816 字,大约阅读时间需要 2 分钟。

题目:(中等)

标签:二叉树、二叉树-深度优先遍历、链表

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( K l o g N ) O(KlogN) O(KlogN) : K为目标链表长度 96ms (99.54%)
Ans 2 (Python)
Ans 3 (Python)

LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。

解法一(存储所有当前可能情况):

在这里插入图片描述

def isSubPath(self, head: ListNode, root: TreeNode) -> bool:    def helper(node, maybe):        # 处理当前节点为空的情况        if not node:            return False        new_maybe = []        for m in maybe:            if node.val == m.val:                if m.next is None:                    return True                new_maybe.append(m.next)        if node.val == head.val:            if head.next is None:                return True            new_maybe.append(head.next)        return helper(node.left, new_maybe) or helper(node.right, new_maybe)    return helper(root, [])

转载地址:https://dataartist.blog.csdn.net/article/details/107604261 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode专项刷题顺序:链表
下一篇:Python中exec()作用域问题解决方案

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月13日 02时07分40秒