예제 #1
0
 def builtTree():
     curr = next(preorder)
     if curr == 'null': return None
     node = TreeNode(int(curr))
     node.left = builtTree()
     node.right = builtTree()
     return node
예제 #2
0
 def reconInAndPost(self, postorder: List[int], inorder: List[int]):
     if not postorder:
         return None
     root, node, j = TreeNode(postorder[-1]), None, len(inorder) - 1
     stack = [root]
     for i in reversed(range(len(postorder) - 1)):
         current = TreeNode(postorder[i])
         while stack and stack[-1].val == inorder[j]:
             node = stack.pop()
             j -= 1
         if not node:
             stack[-1].right = current
         else:
             node.left, node = current, None
         stack.append(current)
     return root
예제 #3
0
 def reconstruct(left, right):
     if left > right:
         return None
     root = TreeNode(preorder.pop(0))
     root.left = reconstruct(left, indexes[root.val] - 1)
     root.right = reconstruct(indexes[root.val] + 1, right)
     return root
예제 #4
0
 def reconInAndPre(self, preorder: List[int], inorder: List[int]):
     if not preorder:
         return None
     root, node, j = TreeNode(preorder[0]), None, 0
     stack = [root]
     for i in range(1, len(preorder)):
         current = TreeNode(preorder[i])
         while stack and stack[-1].val == inorder[j]:
             node = stack.pop()
             j += 1
         if not node:
             stack[-1].left = current
         else:
             node.right = current
             node = None
         stack.append(current)
     return root
예제 #5
0
 def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
     if not preorder: return None
     j, stack = 0, []
     root = TreeNode(preorder[0])
     stack.append(root)
     node = None
     for i in range(1, len(preorder)):
         current = TreeNode(preorder[i])
         while stack and stack[-1].val == inorder[j]:
             node = stack.pop()
             j += 1
         if node:
             node.right = current
             node = None
         else:
             stack[-1].left = current
         stack.append(current)
     return root
예제 #6
0
 def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
     if not inorder: return None
     j, node = len(inorder) - 1, None
     root = TreeNode(postorder[-1])
     stack = []
     stack.append(root)
     for i in range(len(postorder) - 2, -1, -1):
         current = TreeNode(postorder[i])
         while stack and stack[-1].val == inorder[j]:
             node = stack.pop()
             j -= 1
         if node:
             node.left = current
             node = None
         else:
             stack[-1].right = current
         stack.append(current)
     return root
예제 #7
0
 def helper(left: ListNode, right: ListNode) -> TreeNode:
     if left == right: return None
     fast = slow = last = left
     while fast != right and fast.next != right:
         fast = fast.next.next
         last = slow
         slow = slow.next
     current = TreeNode(slow.val)
     current.left = helper(left, last)
     current.right = helper(slow.next, right)
     return current
예제 #8
0
        def generate(start: int, end: int) -> List[TreeNode]:
            if start > end:
                return [None]
            ans = []
            for i in range(start, end + 1):
                left, right = generate(start, i - 1), generate(i + 1, end)

                for l in left:
                    for r in right:
                        current = TreeNode(i)
                        current.left = l
                        current.right = r
                        ans.append(current)
            return ans
예제 #9
0
    def deserialize(self, data):
        """Decodes your encoded data to tree.

        :type data: str
        :rtype: TreeNode
        """
        if not data: return None
        items = deque(data.split(','))
        root, n = TreeNode(items.popleft()), len(items)
        nodeQ = deque([root])
        front = 0
        while items:
            curr = nodeQ.popleft()
            item = items.popleft()
            if item != "null":
                curr.left = TreeNode(int(item))
                nodeQ.append(curr.left)
            if not items: break
            item = items.popleft()
            if item != "null":
                curr.right = TreeNode(int(item))
                nodeQ.append(curr.right)
        return root
예제 #10
0
[2]
[1,2]
[2,1]'''


class _98_Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        stack = []
        last = None
        node = root
        while node:
            stack.append(node)
            node = node.left
        while stack:
            current = stack.pop()
            if last is not None and current.val <= last:
                return False
            last = current.val
            current = current.right
            while current:
                stack.append(current)
                current = current.left
        return True


if __name__ == '__main__':
    instance = _98_Solution
    n0, n1, n2 = TreeNode(0), TreeNode(-1), TreeNode(3)
    n0.right = n1
    assert instance.isValidBST(instance, n0) == False
예제 #11
0
    #
    #     if p == q: return p
    #     if root == p:
    #         if find(root.left, q) or find(root.right, q):
    #             return root
    #     elif find(root.left, p):
    #         if root == q or find(root.right, q): return root
    #         return self.lowestCommonAncestor( root.left, p, q)
    #     else:
    #         if root == q or find(root.left, q): return root
    #         return self.lowestCommonAncestor( root.right, p, q)


if __name__ == '__main__':
    instance = _236_Solution
    n0 = TreeNode(3)
    n1 = TreeNode(5)
    n2 = TreeNode(1)
    n3 = TreeNode(6)
    n4 = TreeNode(2)
    n5 = TreeNode(0)
    n6 = TreeNode(8)
    n7 = TreeNode(7)
    n8 = TreeNode(4)
    n0.left = n1
    n0.right = n2
    n1.left = n3
    n1.right = n4
    n2.left = n5
    n2.right = n6
    n4.left = n7