1. 103. Binary Tree Zigzag Level Order Traversal

    # 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
  2. 102. Binary Tree Level Order Traversal

    import 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 ...
    read more
  3. 101. Symmetric Tree

    # 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
  4. 100. Same Tree

    # 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 ...
    read more
  5. 98. Validate Binary Search Tree

    # 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 ...
    read more
  6. 96. Unique Binary Search Trees

    class 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 ...
    read more
  7. 95. Unique Binary Search Trees II

    # 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 ...
    read more
  8. 94. Binary Tree Inorder Traversal

    # 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 ...
    read more
  9. 93. Restore IP Addresses

    class 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 ...
    read more
  10. 92. Reverse Linked List II

    # 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 ...
    read more

« Page 8 / 22 »

blogroll

social