"""
# Definition for a QuadTree node.
class Node(object):
def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
self.val = val
self.isLeaf = isLeaf
self.topLeft = topLeft
self.topRight = topRight
self.bottomLeft = bottomLeft
self.bottomRight = bottomRight
"""
class Solution(object):
def intersect(self, q1, q2):
"""
:type q1: Node
:type q2: Node
:rtype: Node
"""
if q1.isLeaf:
return q1 if q1.val else q2
if q2.isLeaf:
return q2 if q2.val else q1 q1.topLeft = self.intersect(q1.topLeft, q2.topLeft)
q1.topRight = self.intersect(q1.topRight, q2.topRight)
q1.bottomLeft = self.intersect(q1.bottomLeft, q2.bottomLeft)
q1.bottomRight = self.intersect(q1.bottomRight, q2.bottomRight) if q1.topLeft.isLeaf and q1.topRight.isLeaf and q1.bottomLeft.isLeaf and q1.bottomRight.isLeaf:
if q1.topLeft.val == q1.topRight.val == q1.bottomLeft.val == q1.bottomRight.val:
q1.isLeaf = True
q1.val = q1.topLeft.val
return q1

从网上寻找的代码。

leetcode558的更多相关文章

随机推荐

  1. 【剑指offer】之字形打印二叉树,C++实现

    原创文章,转载请注明出处! 本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 1.题目 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打 ...

  2. C#与sqlserver开发问题

    最近不停的在考虑C#读取数据性能问题第一种使用ado拼接sql连接数据库第二种使用ado调用存储过程第三种使用entityframework加linq第四种使用反射IList<T> 1.从 ...

  3. object references an unsaved transient instance【异常】

    [异常提示] TransientObjectException: object references an unsaved transient instance -save the transient ...

  4. BZOJ1229 USACO2008 Nov toy 玩具 【三分+贪心】*

    BZOJ1229 USACO2008 Nov toy 玩具 Description 玩具 [Chen Hu, 2006] Bessie的生日快到了, 她希望用D (1 <= D <= 10 ...

  5. C#/.NET 中推荐的 Dispose 模式的实现

    如果你觉得你的类需要实现 IDisposable 接口,还是需要注意一些坑的.不过前人准备了 Dispose 模式 供我们参考,最大程度避免这样的坑. C#程序中的 Dispose 方法,一旦被调用了 ...

  6. webpack 简单使用

    备注:  使用yarn 结合npm 模块进行简单项目开发 1. 安装 yarn init yarn add webpack --dev yarn global add live-server 2. 添 ...

  7. python 有关引用的一些问题

    python 有关引用的一些问题 print id.__doc__ ​ id(object) -> integer Return the identity of an object. This ...

  8. Maven使用中的常见问题整理

    1.更新eclipse的classpath加入新依赖  1.在dependencyManagement里面加入包括版本在内的依赖信息,如: <dependency> <groupId ...

  9. 安装 LAMP

    卸载 并安装 MYSQL rpm -qa | grep mysql rpm -e mysql-libs--.el6.x86_64 -.el6.x86_64 cd /usr/local/src/ wge ...

  10. 关于angular.extend的用法

    ng中的ng-function中会有些方法,便于我们进行js代码的编写 关于angular.extend(dst, src);通过从src对象复制所有属性到dst来扩展目标对象dst.你可以指定多个s ...