示例#1
0
 def constructMaximumBinaryTree(self, nums):
     if len(nums) == 0:
         return None
     index = nums.index(max(nums))
     root = TreeNode(nums[index])
     root.left = self.constructMaximumBinaryTree(nums[0:index])
     root.right = self.constructMaximumBinaryTree(nums[index + 1:])
     return root
示例#2
0
def _buildSubtree(rootLabel, children):
    rootNode = TreeNode(rootLabel)
    if children:
        for childDict in children:
            childLabel, childrenOfChild = childDict.iteritems().next()
            childNode = _buildSubtree(childLabel, childrenOfChild)
            rootNode.add_child(childNode)
    return rootNode
 def sortedArrayToBST(self, nums):
     if not nums:
         return None
     mid = nums[len(nums) // 2]
     idx = len(nums) // 2
     root = TreeNode(mid)
     root.left = self.sortedArrayToBST(nums[:idx])
     root.right = self.sortedArrayToBST(nums[idx + 1:])
     return root
 def buildTree(self, preorder, inorder):
     if len(preorder) == 0:
         return None
     else:
         root = TreeNode(preorder[0])
         index = inorder.index(preorder[0])
         left = index
         root.left = self.buildTree(preorder[1:1 + left], inorder[0:index])
         root.right = self.buildTree(preorder[1 + left:],
                                     inorder[index + 1:])
         return root
示例#5
0
 def buildTree(self, inorder, postorder):
     if len(postorder) == 0:
         return None
     else:
         root = TreeNode(postorder[-1])
         index = inorder.index(postorder[-1])
         left = index
         root.left = self.buildTree(inorder[0:index], postorder[:left])
         root.right = self.buildTree(inorder[index + 1:],
                                     postorder[left:-1])
         return root
 def sortedArrayToBST(self, nums):
     if len(nums) == 0:
         return None
     elif len(nums) == 1:
         return TreeNode(nums[0])
     else:
         index = int(len(nums) / 2)
         root = TreeNode(nums[index])
         root.left = self.sortedArrayToBST(nums[0:index])
         root.right = self.sortedArrayToBST(nums[index + 1:])
         return root
示例#7
0
 def mergeTrees(self, t1, t2):
     if t1 is None and t2 is None:
         return None
     elif t1 is None:
         return t2
     elif t2 is None:
         return t1
     else:
         root = TreeNode(t1.val + t2.val)
         root.left = self.mergeTrees(t1.left, t2.left)
         root.right = self.mergeTrees(t1.right, t2.right)
         return root
 def insertIntoBST(self, root, val):
     t = root
     while root is not None and root.val != val:
         if root.val < val:
             if root.right is None:
                 root.right = TreeNode(val)
                 break
             root = root.right
         else:
             if root.left is None:
                 root.left = TreeNode(val)
                 break
             root = root.left
     return t
示例#9
0
    def deserialize(self, data):

        nums = eval(data)
        if len(nums) == 0:
            return None
        tree = [TreeNode(n) for n in nums]
        f = 0
        i = 1
        while i < len(tree):
            if tree[f].val is None:
                f += 1
                continue
            if tree[i].val is not None:
                tree[f].left = tree[i]
            else:
                tree[f].left = None
            i += 1
            if i == len(tree):
                break
            if tree[i].val is not None:
                tree[f].right = tree[i]
            else:
                tree[f].right = None
            i += 1
            f += 1
        return tree[0]
示例#10
0
    def _init_head(self, start, stop, value):
        start_node = Node()
        start_node.index = start
        start_node.data_val = value
        start_node.next_values = value

        self.bst = TreeNode(start_node.index, start_node)
        self.head_node = start_node

        end_node = Node()
        end_node.index = stop
        end_node.data_val = value
        end_node.prev_node = self.head_node

        self.bst.insert(end_node.index, end_node)
        self.head_node.next_node = end_node
 def bulit(self,s,e):
     re=[]
     for i in range(s,e+1):
         left=self.bulit(s,i-1)
         right=self.bulit(i+1,e)
         if len(left) == 0:
             left.append(None)
         if len(right)==0:
             right.append(None)
         for l in left:
             for r in right:
                 root=TreeNode(i)
                 root.left=l
                 root.right=r
                 re.append(root)
     return re
        r=root
        for i in range(min(len(left),len(right))):
            if left[i]==right[i]:
                r=left[i]
        return r
    def find(self,root,p,parent):
        parent.append(root)
        if root is None:
            return False
        if root.val==p.val:
            return True
        if self.find(root.left,p,parent):
            return True
        else:
            del parent[-1]
        if self.find(root.right,p,parent):
            return True
        else:
            del parent[-1]

s=Solution()

test=[
{"input":[deserialize([3,5,1,6,2,0,8,None,None,7,4]),TreeNode(5),TreeNode(4)],"output": 6},

]
for t in test:
    r=s.lowestCommonAncestor(t['input'][0],t['input'][1],t['input'][2])
    if r!=t['output']:
        print("error:"+str(t)+" out:"+str(r))
示例#13
0
class SLinkedList:
    def __init__(self):
        self.head_node = None
        self.bst = None

    def _init_head(self, start, stop, value):
        start_node = Node()
        start_node.index = start
        start_node.data_val = value
        start_node.next_values = value

        self.bst = TreeNode(start_node.index, start_node)
        self.head_node = start_node

        end_node = Node()
        end_node.index = stop
        end_node.data_val = value
        end_node.prev_node = self.head_node

        self.bst.insert(end_node.index, end_node)
        self.head_node.next_node = end_node

    def max_value(self):
        max_data = 0
        current_node = self.head_node
        while current_node is not None:
            if current_node.data_val > max_data:
                max_data = current_node.data_val
            current_node = current_node.next_node
        return max_data

    @timer_func
    def _find_left_node(self, index):
        if index < self.head_node.index:
            return None
        """
        current_node = self.head_node
        prev_node = None
        while current_node is not None:
            if current_node.index <= index:
                prev_node = current_node
                current_node = current_node.next_node
                continue

            return current_node.prev_node

        return prev_node
        """
        tree_node = self.bst.find_left_nearest_node(index)
        return tree_node if not tree_node else tree_node.link_obj

    def _insert_node(self, left, index) -> Node:
        new_node = Node()
        new_node.index = index
        new_node.prev_node = left if left else None
        new_node.next_node = left.next_node if left else self.head_node

        self.bst.insert(new_node.index, new_node)

        if not left:
            self.head_node.prev_node = new_node
            self.head_node = new_node
            return new_node

        if left.next_node:
            left.next_node.prev_node = new_node
        left.next_node = new_node

        return new_node

    def _insert_interval(self, start, stop, value):
        left_node_a = self._find_left_node(start)
        if left_node_a and left_node_a.index == start:
            new_start_node = left_node_a
        else:
            new_start_node = self._insert_node(left_node_a, start)

        if left_node_a and left_node_a.index != start:
            new_start_node.data_val = value + left_node_a.next_values
            new_start_node.next_values = value + left_node_a.next_values
        else:
            new_start_node.data_val += value
            new_start_node.next_values += value

        left_node_b = self._find_left_node(stop)
        if left_node_b and left_node_b.index == stop:
            new_end_node = left_node_b
        else:
            new_end_node = self._insert_node(left_node_b, stop)

        update_node = new_start_node.next_node
        self._update_interval(new_end_node, update_node, value)

        if left_node_b.index != stop:
            new_end_node.data_val = left_node_b.next_values
            new_end_node.next_values = left_node_b.next_values - value
        else:
            new_end_node.data_val += value

    @staticmethod
    @timer_func
    def _update_interval(new_end_node, update_node, value):
        while update_node and update_node != new_end_node:
            update_node.data_val += value
            update_node.next_values += value
            update_node = update_node.next_node

    def _insert_point(self, index, value):
        left_node_a = self._find_left_node(index)
        if left_node_a and left_node_a.index == index:
            new_start_node = left_node_a
        else:
            new_start_node = self._insert_node(left_node_a, index)

        if left_node_a and left_node_a.index != index:
            new_start_node.data_val = value + left_node_a.next_values
            new_start_node.next_values = left_node_a.next_values
        else:
            new_start_node.data_val += value

    def add_interval(self, start, stop, value):
        if not self.head_node:
            self._init_head(start, stop, value)
            return

        if start == stop:
            self._insert_point(start, value)
        else:
            self._insert_interval(start, stop, value)

    def print(self):
        print_node = self.head_node
        print('------------------------------------')
        while print_node is not None:
            print(print_node)
            print_node = print_node.next_node
        print('Max value: ', self.max_value())