LeetCode题解(0993):判断节点是否为堂兄弟节点(Python)
发布日期:2021-06-29 19:55:05 浏览次数:3 分类:技术文章

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

题目:(简单)

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( N ) O(N) O(N) 40ms (83.55%)
Ans 2 (Python)
Ans 3 (Python)

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

解法一:

def __init__(self):    self.depth = None    self.father = Nonedef isCousins(self, root: TreeNode, x: int, y: int) -> bool:    def helper(node, depth=0, father=None):        if node:            if node.val == x or node.val == y:                if self.depth is None:                    self.depth = depth + 1                    self.father = father                else:                    if self.depth == depth + 1 and self.father != father:                        return True                    else:                        return False            return helper(node.left, depth + 1, node.val) or helper(node.right, depth + 1, node.val)    return helper(root)

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

上一篇:LeetCode题解(0997):找到小镇的法官(Python)
下一篇:LeetCode题解(0989):数组形式的整数加法(Python)

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月26日 19时38分35秒