Skip to content

Commit 93381e5

Browse files
committed
Recover Binary Search Tree/ Surrounded Regions
1 parent 1a7ca41 commit 93381e5

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

099 Recover Binary Search Tree.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
Two elements of a binary search tree (BST) are swapped by mistake.
3+
4+
Recover the tree without changing its structure.
5+
6+
Note:
7+
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
8+
'''
9+
10+
# Definition for a binary tree node.
11+
class TreeNode(object):
12+
def __init__(self, x):
13+
self.val = x
14+
self.left = None
15+
self.right = None
16+
17+
18+
class Solution(object):
19+
def __init__(self):
20+
self.node1 = None
21+
self.node2 = None
22+
self.pre = None
23+
24+
def recoverTree(self, root):
25+
"""
26+
:type root: TreeNode
27+
:rtype: void Do not return anything, modify root in-place instead.
28+
"""
29+
self.__scan(root)
30+
self.node1.val, self.node2.val = self.node2.val, self.node1.val
31+
32+
def __scan(self, root):
33+
if root is None:
34+
return
35+
self.__scan(root.left)
36+
if self.pre is not None:
37+
if root.val < self.pre.val:
38+
if self.node1 is None:
39+
self.node1 = self.pre
40+
self.node2 = root
41+
else:
42+
self.node2 = root
43+
self.pre = root
44+
self.__scan(root.right)
45+
46+
47+
if __name__ == "__main__":
48+
None

130 Surrounded Regions.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'''
2+
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
3+
4+
A region is captured by flipping all 'O's into 'X's in that surrounded region.
5+
6+
For example,
7+
X X X X
8+
X O O X
9+
X X O X
10+
X O X X
11+
After running your function, the board should be:
12+
13+
X X X X
14+
X X X X
15+
X X X X
16+
X O X X
17+
'''
18+
19+
class Solution(object):
20+
def solve(self, board):
21+
"""
22+
:type board: List[List[str]]
23+
:rtype: void Do not return anything, modify board in-place instead.
24+
"""
25+
if not board or not board[0]:
26+
return
27+
n = len(board)
28+
m = len(board[0])
29+
queue = []
30+
# Get all 'O' on edge
31+
for i in range(n):
32+
for j in range(m):
33+
if ((i in (0, n - 1)) or (j in (0, m - 1))) and board[i][j] == 'O':
34+
queue.append((i, j))
35+
# Mark all 'O' which can connect to 'O' on edge
36+
while queue:
37+
r, c = queue.pop(0)
38+
if 0 <= r < n and 0 <= c < m and board[r][c] == 'O':
39+
board[r][c] = 'M'
40+
if r - 1 >= 0 and board[r - 1][c] == 'O':
41+
queue.append((r - 1, c))
42+
if r + 1 < n and board[r + 1][c] == 'O':
43+
queue.append((r + 1, c))
44+
if c - 1 >= 0 and board[r][c - 1] == 'O':
45+
queue.append((r, c - 1))
46+
if c + 1 < m and board[r][c + 1] == 'O':
47+
queue.append((r, c + 1))
48+
# Update characters
49+
for i in range(n):
50+
for j in range(m):
51+
if board[i][j] == 'M':
52+
board[i][j] = 'O'
53+
else:
54+
board[i][j] = 'X'
55+
56+
57+
if __name__ == "__main__":
58+
board = [
59+
['X', 'X', 'X', 'X'],
60+
['X', 'O', 'O', 'X'],
61+
['X', 'X', 'O', 'X'],
62+
['X', 'O', 'X', 'X']
63+
]
64+
expected_board = [
65+
['X', 'X', 'X', 'X'],
66+
['X', 'X', 'X', 'X'],
67+
['X', 'X', 'X', 'X'],
68+
['X', 'O', 'X', 'X']
69+
]
70+
Solution().solve(board)
71+
assert board == expected_board

0 commit comments

Comments
 (0)