1. Implement Queue Using Stacks [59ms]
class Queue(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.stack.append(x)
def pop(self):
"""
:rtype: nothing
"""
if self.empty(): return None
tmp = []
while not self.empty():
tmp.append(self.stack.pop())
val = tmp.pop()
while len(tmp) != 0:
self.stack.append(tmp.pop())
def peek(self):
"""
:rtype: int
"""
if not self.empty():
return self.stack[0]
else:
return None
def empty(self):
"""
:rtype: bool
"""
return True if len(self.stack) == 0 else False
2. Binary Path Tree [49ms]
class Solution:
import copy
def binaryTreePaths(self, root):
if root == None:
return []
ans = []
self.goDown(root, [], ans)
for i in range(len(ans)):
ans[i] = "->".join(ans[i])
return ans
def goDown(self, node, a, ans):
if node.left == None and node.right == None:
a.append(str(node.val))
tmp = copy.copy(a)
ans.append(tmp)
return
a.append(str(node.val))
if node.left != None:
self.goDown(node.left, a, ans)
a.pop()
if node.right != None:
self.goDown(node.right, a, ans)
a.pop()
return
3. Valid Parentheses [49ms]
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
left = {"(":0, "{":1, "[":2}
right = {")":0, "}":1, "]":2}
stack = []
i = 0
while i < len(s):
if s[i] in left:
stack.append(s[i])
else:
if len(stack) != 0:
l = stack.pop()
if left[l] != right[s[i]]:
return False
else:
return False
i += 1
if len(stack) != 0: return False
return True
4. Path Sum [89ms]
class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if root == None:
return False
ans = self.goDown(root, 0, sum)
return ans
def goDown(self, node, tmp, sum):
if node == None:
return False
tmp2 = tmp + node.val
if node.left or node.right:
return self.goDown(node.left, tmp2, sum) or self.goDown(node.right, tmp2, sum)
else:
return tmp2 == sum
5. Validate Binary Tree [59ms]
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root == None:
return True
inord = []
self.inorder(root, inord)
if len(inord) == 1:
return True
i = 0
while i + 1 < len(inord):
if inord[i] >= inord[i+1]:
return False
i += 1
if inord[-1] <= inord[-2]:
return False
return True
def inorder(self, node, inord):
if node == None:
return
self.inorder(node.left, inord)
inord.append(node.val)
self.inorder(node.right, inord)
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return self.valid(root, -2147483649, 2147483649)
def valid(self, node, minval, maxval):
if node == None:
return True
if minval < node.val < maxval:
return (self.valid(node.left, minval, node.val)) and (self.valid(node.right, node.val, maxval))
else:
return False
6. Same Tree [32ms]
class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if q == None and p == None:
return True
elif q != None and p != None:
if q.val == p.val:
left = self.isSameTree(p.left, q.left)
right = self.isSameTree(p.right, q.right)
return (left and right)
else:
return False
else:
return False
7. Implement Stack Using Queues [56ms]
class Stack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.queue = []
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.queue.insert(0, x)
def pop(self):
"""
:rtype: nothing
"""
if self.empty():
return
lth = len(self.queue) - 1
while lth:
self.queue.insert(0, self.queue.pop())
lth -= 1
self.queue.pop()
def top(self):
"""
:rtype: int
"""
if self.empty():
return None
else:
return self.queue[0]
def empty(self):
"""
:rtype: bool
"""
if len(self.queue) == 0:
return True
else:
return False