Skip to content

Commit 64710a1

Browse files
committed
Solution of Rotten Oranges and Coursin in Binary Tree
1 parent e141c4a commit 64710a1

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

900-1000q/993.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'''
2+
In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.
3+
4+
Two nodes of a binary tree are cousins if they have the same depth, but have different parents.
5+
6+
We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.
7+
8+
Return true if and only if the nodes corresponding to the values x and y are cousins.
9+
10+
Input: root = [1,2,3,4], x = 4, y = 3
11+
Output: false
12+
'''
13+
14+
# Definition for a binary tree node.
15+
# class TreeNode(object):
16+
# def __init__(self, x):
17+
# self.val = x
18+
# self.left = None
19+
# self.right = None
20+
21+
class Solution(object):
22+
def adjacent(self, root, node1, node2):
23+
if not root:
24+
return False
25+
26+
value = False
27+
if (root.right and root.left):
28+
value = ((root.left.val == node1 and root.right.val == node2) or
29+
(root.left.val == node2 and root.right.val == node1))
30+
31+
return (value or
32+
self.adjacent(root.left, node1, node2) or
33+
self.adjacent(root.right, node1, node2))
34+
35+
def _level(self, root, node, level):
36+
if not root:
37+
return 0
38+
if root.val == node:
39+
return level
40+
41+
left_level = self._level(root.left, node, level+1)
42+
if left_level != 0:
43+
return left_level
44+
return self._level(root.right, node, level+1)
45+
46+
def isCousins(self, root, x, y):
47+
"""
48+
:type root: TreeNode
49+
:type x: int
50+
:type y: int
51+
:rtype: bool
52+
"""
53+
if ((self._level(root, x, 1) == self._level(root, y, 1)) and not self.adjacent(root, x, y)):
54+
return True
55+
return False

900-1000q/994.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'''
2+
In a given grid, each cell can have one of three values:
3+
4+
the value 0 representing an empty cell;
5+
the value 1 representing a fresh orange;
6+
the value 2 representing a rotten orange.
7+
Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.
8+
9+
Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1 instead.
10+
'''
11+
12+
class Solution(object):
13+
def valid(self, row, col, row_size, col_size):
14+
return row >= 0 and col >= 0 and row < row_size and col < col_size
15+
16+
def orangesRotting(self, grid):
17+
"""
18+
:type grid: List[List[int]]
19+
:rtype: int
20+
"""
21+
queue = []
22+
for row_index in range(len(grid)):
23+
for col_index in range(len(grid[0])):
24+
if grid[row_index][col_index] == 2:
25+
queue.append((row_index, col_index))
26+
27+
result = 0
28+
queue.append((-1, -1))
29+
while queue:
30+
flag = False
31+
print queue
32+
while(queue[0][0] != -1 and queue[0][1] != -1):
33+
(row, col) = queue[0]
34+
if self.valid(row+1, col, len(grid), len(grid[0])) and grid[row+1][col] == 1 :
35+
if not flag:
36+
result += 1
37+
flag =True
38+
grid[row+1][col] = 2
39+
row += 1
40+
queue.append((row, col))
41+
row -= 1
42+
if self.valid(row-1, col, len(grid), len(grid[0])) and grid[row-1][col] == 1 :
43+
if not flag:
44+
result += 1
45+
flag =True
46+
grid[row-1][col] = 2
47+
row -= 1
48+
queue.append((row, col))
49+
row += 1
50+
if self.valid(row, col+1, len(grid), len(grid[0])) and grid[row][col+1] == 1 :
51+
if not flag:
52+
result += 1
53+
flag =True
54+
grid[row][col+1] = 2
55+
col += 1
56+
queue.append((row, col))
57+
col -= 1
58+
if self.valid(row, col-1, len(grid), len(grid[0])) and grid[row][col-1] == 1 :
59+
if not flag:
60+
result += 1
61+
flag =True
62+
grid[row][col-1] = 2
63+
col -= 1
64+
queue.append((row, col))
65+
col += 1
66+
queue.pop(0)
67+
queue.pop(0)
68+
if queue:
69+
queue.append((-1, -1))
70+
for row_index in range(len(grid)):
71+
for col_index in range(len(grid[0])):
72+
if grid[row_index][col_index] == 1:
73+
return -1
74+
return result

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
77
##### [Problems 900-1000](./900-1000q/)
88
| # | Title | Solution | Difficulty |
99
|---| ----- | -------- | ---------- |
10+
|994|[Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [Python] | (./900-1000q/994.py)|Easy|
11+
|993|[Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [Python]|(./900-1000q/993.py)|Easy|
1012
|991|[Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Python](./900-1000q/991.py)|Medium|
1113
|990|[Satisfiability of Equality Equations](https://leetcode.com/problems/satisfiability-of-equality-equations/) | [Python](./900-1000q/990.py)|Medium|
1214
|989|[Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/)|[Python](./900-1000q/989.py)|Easy|

0 commit comments

Comments
 (0)