def AddTreeNode(self, preorder, prestart, preend, inorder, instart, inend):
     nodesLen = preend - prestart
     if nodesLen == 0: return None
     elif nodesLen == 1: return TreeNode(preorder[prestart])
     root = TreeNode(preorder[prestart])
     rootIdx = inorder.index(preorder[prestart]) - instart
     root.left = self.AddTreeNode(preorder, prestart+1, prestart+rootIdx+1, inorder, instart, instart+rootIdx)
     root.right = self.AddTreeNode(preorder, prestart+rootIdx+1, preend, inorder, instart+rootIdx+1, inend)
     return root
 def AddTreeNode(self, preorder, prestart, preend, inorder, instart, inend):
     nodesLen = preend - prestart
     if nodesLen == 0: return None
     elif nodesLen == 1: return TreeNode(preorder[prestart])
     root = TreeNode(preorder[prestart])
     rootIdx = inorder.index(preorder[prestart]) - instart
     root.left = self.AddTreeNode(preorder, prestart + 1,
                                  prestart + rootIdx + 1, inorder, instart,
                                  instart + rootIdx)
     root.right = self.AddTreeNode(preorder, prestart + rootIdx + 1, preend,
                                   inorder, instart + rootIdx + 1, inend)
     return root
コード例 #3
0
 def solution(self, nums):
     """
     :type nums: List[int]
     :rtype: TreeNode
     """
     numsLen = len(nums)
     if numsLen == 0:
         return None
     elif numsLen == 1:
         return TreeNode(nums[0])
     else:
         root = TreeNode(nums[numsLen / 2])
         root.left = self.solution(nums[:numsLen / 2])
         root.right = self.solution(nums[numsLen / 2 + 1:])
     return root
コード例 #4
0
def main():
    solver = Solver()
    root = TreeNode(3)
    root.left = TreeNode(9)
    root.right = TreeNode(20)
    root.right.left = TreeNode(15)
    root.right.right = TreeNode(7)

    print(solver.max_depth(root))
 def solution(self, preorder, inorder): # not memory efficient since every recursive created new preorder/inorder lists
     """
     :type preorder: List[int]
     :type inorder: List[int]
     :rtype: TreeNode
     """
     nodeLen = len(preorder)
     if nodeLen == 0: return None
     elif nodeLen == 1: return TreeNode(preorder[0])
     root = TreeNode(preorder[0])
     for rootIdx in range(nodeLen):
         if inorder[rootIdx] == preorder[0]: break
     root.left = self.solution(preorder[1:rootIdx+1], inorder[:rootIdx])
     root.right = self.solution(preorder[rootIdx+1:], inorder[rootIdx+1:])
     return root
 
     
 def solution(
     self, preorder, inorder
 ):  # not memory efficient since every recursive created new preorder/inorder lists
     """
     :type preorder: List[int]
     :type inorder: List[int]
     :rtype: TreeNode
     """
     nodeLen = len(preorder)
     if nodeLen == 0: return None
     elif nodeLen == 1: return TreeNode(preorder[0])
     root = TreeNode(preorder[0])
     for rootIdx in range(nodeLen):
         if inorder[rootIdx] == preorder[0]: break
     root.left = self.solution(preorder[1:rootIdx + 1], inorder[:rootIdx])
     root.right = self.solution(preorder[rootIdx + 1:],
                                inorder[rootIdx + 1:])
     return root
コード例 #7
0
 def find_pentagon(self):
     for vertex in self.graph.vertices:
         root_node = TreeNode(vertex.id)
         nodes = [root_node]
         while len(nodes) > 0:
             node = nodes.pop(0)
             path = node.get_path()
             v_a_id = node.value
             v_a = self.graph.get_vertex(v_a_id)
             for v_b in v_a.neighbors:
                 if not v_b.id in path and len(path) < 5:
                     child = TreeNode(v_b.id)
                     node.add_child(child)
                     nodes.append(child)
                 elif len(path) == 5 and v_b == vertex:
                     # check path
                     # a valid pentagon has not internal edges on itself
                     if self.check_pentagon(path):
                         return path
     return None
コード例 #8
0
elif titleNum == 227: # Basic Calculator II, Hard, Implementation not efficient
    from BasicCalculatorII import Calculate
    bc2 = Calculate()
    print bc2.solution('0-0')
elif titleNum == 229:
    from MajorityElementII import MajorityElementII # Majority Element II, Hard, Moore's algorithm variation
    me2 = MajorityElementII()
    print me2.solution([8, 8, 7, 7, 7])
elif titleNum == 233:
    from NumberToDigitalOne import CountDigitOne # Number of Digit One, Hard, tricky
elif titleNum == 263:
    from UglyNumber import IsUgly # Ugly Number
elif titleNum == 264:
    from UglyNumberII import NthUglyNumber # Ugly Number II, Hard, Smart way to do it
    nun = NthUglyNumber()
    print nun.solution(7)
elif titleNum == 273:
    from IntegerToEnglishWords import NumberToWords # Integer To English Words
elif titleNum == 274:
    from HIndex import HIndex # H-Index
elif titleNum == 297:  # Serialize and Deserialize Binary Tree
    from common.TreeNode import TreeNode
    from common.SeDesBinaryTree import Codec
    root = TreeNode(1)
    sdbt = Codec();
    print sdbt.serialize(root)
    sdbt.deserialize(sdbt.serialize(root)).printTreeNodeVal()
else:
    print 'Do nothing'