Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 73fa4fb

Browse files
committed
2020-03-05
1 parent 427304c commit 73fa4fb

File tree

11 files changed

+327
-0
lines changed

11 files changed

+327
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def mirrorTree(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: TreeNode
13+
"""
14+
if not root:
15+
return root
16+
17+
root.left, root.right = self.mirrorTree(root.right), self.mirrorTree(root.left)
18+
19+
return root
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def isSymmetric(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: bool
13+
"""
14+
if not root:
15+
return True
16+
17+
def check(root1, root2):
18+
if not root1 and not root2:
19+
return True
20+
if not root1 or not root2:
21+
return False
22+
return root1.val == root2.val and check(root1.left, root2.right) and check(root1.right, root2.left)
23+
return check(root, root)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution(object):
2+
def spiralOrder(self, matrix):
3+
"""
4+
:type matrix: List[List[int]]
5+
:rtype: List[int]
6+
"""
7+
if not matrix or not matrix[0]:
8+
return matrix
9+
m, n = len(matrix), len(matrix[0])
10+
11+
x, y = 0, 0
12+
state = "r"
13+
cnt = 0
14+
res = []
15+
visited = set()
16+
while cnt < m * n:
17+
res.append(matrix[x][y])
18+
visited.add((x, y))
19+
if state == "r":
20+
if y + 1 < n and (x, y + 1) not in visited:
21+
y += 1
22+
else:
23+
x += 1
24+
state = "d"
25+
elif state == "d":
26+
if x + 1 < m and (x + 1, y) not in visited:
27+
x += 1
28+
else:
29+
y -= 1
30+
state = "l"
31+
elif state == "l":
32+
if y - 1 >= 0 and (x, y - 1) not in visited:
33+
y -= 1
34+
else:
35+
x -= 1
36+
state = "u"
37+
elif state == "u":
38+
if x - 1 >= 0 and (x - 1, y) not in visited:
39+
x -= 1
40+
else:
41+
y += 1
42+
state = "r"
43+
cnt += 1
44+
return res
45+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class MinStack(object):
2+
3+
def __init__(self):
4+
"""
5+
initialize your data structure here.
6+
"""
7+
self.stack = []
8+
self.min_s = []
9+
10+
def push(self, x):
11+
"""
12+
:type x: int
13+
:rtype: None
14+
"""
15+
if not self.stack:
16+
self.stack = [x]
17+
self.min_s = [x]
18+
else:
19+
self.stack.append(x)
20+
if self.min_s[-1] < x:
21+
self.min_s.append(self.min_s[-1])
22+
else:
23+
self.min_s.append(x)
24+
25+
def pop(self):
26+
"""
27+
:rtype: None
28+
"""
29+
self.stack.pop()
30+
self.min_s.pop()
31+
32+
def top(self):
33+
"""
34+
:rtype: int
35+
"""
36+
return self.stack[-1]
37+
38+
def min(self):
39+
"""
40+
:rtype: int
41+
"""
42+
return self.min_s[-1]
43+
44+
45+
# Your MinStack object will be instantiated and called as such:
46+
# obj = MinStack()
47+
# obj.push(x)
48+
# obj.pop()
49+
# param_3 = obj.top()
50+
# param_4 = obj.min()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def validateStackSequences(self, pushed, popped):
3+
"""
4+
:type pushed: List[int]
5+
:type popped: List[int]
6+
:rtype: bool
7+
"""
8+
if not pushed and not popped:
9+
return True
10+
if not pushed or not popped:
11+
return False
12+
stack = []
13+
popped = popped[::-1]
14+
for i, num in enumerate(pushed):
15+
stack.append(num)
16+
while stack and stack[-1] == popped[-1]:
17+
stack.pop()
18+
popped.pop()
19+
return not popped
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def levelOrder(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: List[int]
13+
"""
14+
from collections import deque
15+
if not root:
16+
return []
17+
18+
queue = deque([root])
19+
res = []
20+
while queue:
21+
for _ in range(len(queue)):
22+
cur = queue.popleft()
23+
res.append(cur.val)
24+
if cur.left:
25+
queue.append(cur.left)
26+
if cur.right:
27+
queue.append(cur.right)
28+
return res
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def levelOrder(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: List[List[int]]
13+
"""
14+
from collections import deque
15+
if not root:
16+
return []
17+
18+
queue = deque([root])
19+
res = []
20+
while queue:
21+
tmp = []
22+
for _ in range(len(queue)):
23+
cur = queue.popleft()
24+
tmp.append(cur.val)
25+
if cur.left:
26+
queue.append(cur.left)
27+
if cur.right:
28+
queue.append(cur.right)
29+
res.append(tmp)
30+
return res
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def levelOrder(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: List[List[int]]
13+
"""
14+
from collections import deque
15+
if not root:
16+
return []
17+
18+
queue = deque([root])
19+
res = []
20+
flag = 1
21+
while queue:
22+
tmp = []
23+
for _ in range(len(queue)):
24+
cur = queue.popleft()
25+
tmp.append(cur.val)
26+
if cur.left:
27+
queue.append(cur.left)
28+
if cur.right:
29+
queue.append(cur.right)
30+
if flag:
31+
res.append(tmp)
32+
else:
33+
res.append(tmp[::-1])
34+
flag = 1 - flag
35+
return res
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def verifyPostorder(self, postorder):
3+
"""
4+
:type postorder: List[int]
5+
:rtype: bool
6+
"""
7+
if not postorder or len(postorder) == 1:
8+
return True
9+
10+
for i in range(len(postorder)):
11+
if postorder[i] > postorder[-1]:
12+
break
13+
if any(postorder[j] < postorder[-1] for j in range(i, len(postorder) - 1)):
14+
return False
15+
16+
if i == len(postorder):
17+
# no right subtree
18+
left = postorder[:-1]
19+
right = None
20+
else:
21+
left = postorder[:i]
22+
right = postorder[i:-1]
23+
24+
return self.verifyPostorder(left) and self.verifyPostorder(right)
25+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def pathSum(self, root, summ):
10+
"""
11+
:type root: TreeNode
12+
:type sum: int
13+
:rtype: List[List[int]]
14+
"""
15+
self.res = []
16+
17+
def dfs(node, path, s):
18+
if not node:
19+
return
20+
21+
if not node.left and not node.right:
22+
if s + node.val == summ:
23+
path.append(node.val)
24+
self.res.append(path)
25+
return
26+
27+
dfs(node.left, path + [node.val], s + node.val)
28+
dfs(node.right, path + [node.val], s + node.val)
29+
30+
dfs(root, [], 0)
31+
return self.res
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def findContinuousSequence(self, target):
3+
"""
4+
:type target: int
5+
:rtype: List[List[int]]
6+
"""
7+
from collections import deque
8+
window = deque()
9+
res = []
10+
s = 0
11+
i = 1
12+
while i < target // 2 + 3:
13+
if s == target:
14+
res.append(list(window))
15+
if s <= target:
16+
window.append(i)
17+
s += i
18+
i += 1
19+
elif s > target:
20+
s -= window.popleft()
21+
22+
return res

0 commit comments

Comments
 (0)