# 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 zigzagLevelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if root == None:
return []
queue = [root]
result = []
count = 0
while queue:
newq = []
newr = []
for ...- read more
102. Binary Tree Level Order Traversal
read moreimport Queue # 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 levelOrder(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ if root == None: return [] queue = Queue.Queue() queue.put(root) # record each ...
101. Symmetric 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 isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if root == None: return True return self.help(root.left, root.right) def help(self, p ...
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 ...