# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root == None:
return True
return self.help(root.left, root.right)
def help(self, p ...- read more
100. Same Tree
read more# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def isSameTree(self, p, q): """ :type p: TreeNode :type q: TreeNode :rtype: bool """ if p == None and q == None: return True if p and q ...
98. Validate Binary Search Tree
read more# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def isValidBST(self, root): """ :type root: TreeNode :rtype: bool """ return self.dfs(root, min=float('-inf'), max=float('inf')) def dfs(self, root, min, max ...
96. Unique Binary Search Trees
read moreclass Solution(object): def numTrees(self, n): """ :type n: int :rtype: int """ nums = [0] * (n + 1) nums[0] = 1 nums[1] = 1 if n == 0 or n == 1: return 1 for i in range(2, n + 1): result = 0 for j in range(0, i): result += nums[j] * nums[i ...
95. Unique Binary Search Trees II
read more# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def generateTrees(self, n): """ :type n: int :rtype: List[TreeNode] """ if n == 0: return [] return self.dfs(1, n) def dfs(self, start, end): if ...
94. Binary Tree Inorder Traversal
read more# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def inorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ if root is None: return [] return self.inorderTraversal(root.left) + [root.val]\ + self.inorderTraversal(root ...
93. Restore IP Addresses
read moreclass Solution(object): def restoreIpAddresses(self, s): """ :type s: str :rtype: List[str] """ ans = [] self.helper(ans, s, 4, []) return ['.'.join(x) for x in ans] def helper(self, ans, s, k, temp): if len(s) > k * 3: return '' elif k == 0: ans.append(temp) else: for i in range ...
92. Reverse Linked List II
read more# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def reverseBetween(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ if m == n: return head cons = ListNode(0) cons.next = head cur = cons ...
91. Decode Ways
read moreclass Solution(object): def numDecodings(self, s): """ :type s: str :rtype: int """ code = ['10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26'] if s == "" or s[0] == '0': return 0 dp = [0 for x in range(len(s)+1)] dp[0] = 1 ...
90. Subsets II
read moreclass Solution(object): def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() total = [[]] for i in range(1, len(nums) + 1): total += self.subsets_each(nums, i) return total def subsets_each(self, array, target): self.result = [] for i in range(len(array)): self.helper([array[i ...