classSolution(object): defsubsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ res = [] nums.sort() defhelper(idx,tmp): res.append(tmp) for i inrange(idx,len(nums)): if i > idx and nums[i] == nums[i-1]: continue helper(i+1,tmp+[nums[i]]) helper(0,[]) return res
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None
classSolution(object): defreverseBetween(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ ifnot head or m == n: return head pre = dummy = ListNode(None) dummy.next = head for i inrange(m-1): pre = pre.next cur = pre.next for i inrange(n-m): tmp = cur.next cur.next = tmp.next tmp.next = pre.next pre.next = tmp return dummy.next
classSolution(object): defrestoreIpAddresses(self, s): """ :type s: str :rtype: List[str] """ res = [] n = len(s) defbacktrack(i,tmp,flag): if i == n and flag == 0: res.append(tmp[:-1]) return if flag < 0: return for j inrange(i, i+3): if j < n: if i == j and s[j] == '0': backtrack(j+1,tmp+s[j]+'.',flag-1) break if0 < int(s[i:j+1]) <= 255: backtrack(j+1,tmp+s[i:j+1]+'.',flag-1) backtrack(0,'',4) return res
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None
classSolution(object): definorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ ifnot root: return [] res = [] if root.left: res += self.inorderTraversal(root.left) res.append(root.val) if root.right: res += self.inorderTraversal(root.right) return res
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None
classSolution(object): definorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ ifnot root: return [] res = [] stack = [] p = root while p orlen(stack): while p: stack.append(p) p = p.left if stack: p = stack.pop() res.append(p.val) p = p.right return res
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None
classSolution(object): defgenerateTrees(self, n): """ :type n: int :rtype: List[TreeNode] """ if n == 0: return [] defhelper(nums): ifnot nums: return [None] res = [] for i inrange(len(nums)): for l in helper(nums[:i]): for r in helper(nums[i+1:]): node = TreeNode(nums[i]) node.left = l node.right = r res.append(node) return res return helper(list(range(1,n+1)))
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None
classSolution(object): defisValidBST(self, root): """ :type root: TreeNode :rtype: bool """ stack = [] p = root pre = None while p or stack: while p: stack.append(p) p = p.left if stack: p = stack.pop() if pre and p.val <= pre.val: returnFalse pre = p p = p.right returnTrue
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None
classSolution(object): defrecoverTree(self, root): """ :type root: TreeNode :rtype: None Do not return anything, modify root in-place instead. """ first = second = None pre = None stack = [] p = root while p or stack: while p: stack.append(p) p = p.left if stack: p = stack.pop() ifnot first and pre and pre.val > p.val: first = pre if first and pre and pre.val > p.val: second = p pre = p p = p.right first.val,second.val = second.val,first.val