Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
leetcode: finished #427
Browse files Browse the repository at this point in the history
  • Loading branch information
xqm32 committed Feb 19, 2024
1 parent aa17305 commit a9353f6
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions content/leetcode/2024/2.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,58 @@ title: 2024.2
draft: false
---

# 2024.2.19

```python
#
# @lc app=leetcode.cn id=427 lang=python3
#
# [427] 建立四叉树
#

# @lc code=start
"""
# Definition for a QuadTree node.
class Node:
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:
def construct(self, grid: List[List[int]]) -> "Node":
def is_leaf(x, y, length):
for i in range(x, x + length):
for j in range(y, y + length):
if grid[i][j] != grid[x][y]:
return False
return True

def construct_tree(x, y, length):
if is_leaf(x, y, length):
return Node(grid[x][y] == 1, True, None, None, None, None)
else:
half = length // 2
return Node(
"*",
False,
construct_tree(x, y, half),
construct_tree(x, y + half, half),
construct_tree(x + half, y, half),
construct_tree(x + half, y + half, half),
)

return construct_tree(0, 0, len(grid))


# @lc code=end
```

# 2024.2.18

```python
Expand Down

0 comments on commit a9353f6

Please sign in to comment.