def buildTree2(self, inorder, postorder): if postorder is None or inorder is None or len(postorder) == 0: return None root = TreeNode(postorder[-1]) root_index_in_inorder = inorder.index(postorder[-1]) root.left = self.buildTree(inorder[0:root_index_in_inorder], postorder[0:root_index_in_inorder]) root.right = self.buildTree(inorder[root_index_in_inorder + 1:], postorder[root_index_in_inorder:-1]) return root
def generateTrees(self, n): if n == 1: return TreeNode(0) if n == 2: t1 = TreeNode(1) t1.right = TreeNode(2) t3 = TreeNode(2) t3.left = TreeNode(1) return [t1,t3] dp = ['#',[[1]],[[2,1],[1,'#',2]]] for i in xrange(3,n+1): res = [] for j in xrange(0,n): result = [j] if j == 0: result.append('#') result.extend([m+1 for m in dp[i-1]]) else: pass
def buildTree(self, preorder, inorder): if preorder is None or inorder is None or len(preorder) == 0: return None if len(preorder) != len(inorder): return None if len(preorder) == 1: return TreeNode(preorder[0]) root = TreeNode(preorder[0]) root_index_in_inorder = 0 for i in xrange(len(inorder)): if inorder[i] == preorder[0]: root_index_in_inorder = i break if root_index_in_inorder == 0: root.right = self.buildTree(preorder[1:], inorder[1:]) elif root_index_in_inorder == len(inorder) - 1: root.left = self.buildTree(preorder[1:], inorder[0:root_index_in_inorder]) else: root.left = self.buildTree(preorder[1:root_index_in_inorder + 1], inorder[0:root_index_in_inorder]) root.right = self.buildTree(preorder[root_index_in_inorder + 1:], inorder[root_index_in_inorder + 1:]) return root
if order: result.append([node.val for node in stack]) order = False else: result.append([node.val for node in stack]) result[-1].reverse() order = True stack2 = [] for i in stack: if i.left is not None: stack2.append(i.left) if i.right is not None: stack2.append(i.right) stack = stack2 return result n1 = TreeNode(1) n2 = TreeNode(2) n3 = TreeNode(3) n4 = TreeNode(4) n5 = TreeNode(5) n6 = TreeNode(6) n7 = TreeNode(7) n8 = TreeNode(8) n1.left = n2 n1.right = n3 n2.left = n4 n2.right = n5 n3.left = n6 n3.right = n7
def __calc__(head, n): if head is None: return None if head.next is None: return TreeNode(head.val) if head.next.next is None: t1 = TreeNode(head.val) t2 = TreeNode(head.next.val) t1.right = t2 return t1 if head.next.next.next is None: t1 = TreeNode(head.val) t2 = TreeNode(head.next.val) t3 = TreeNode(head.next.next.val) t2.right = t3 t2.left = t1 return t2
node = stack.pop(0) tmp = node.left or node.right while tmp is not None: if node.left is not None and tmp is node.left and node.right is not None: node.left.next = node.right else: next = node.next found = False while next is not None: if next.left is not None: tmp.next = next.left found = True break elif next.right is not None: tmp.next = next.right found = True break else: next = next.next if not found: tmp.next = None stack.append(tmp) if tmp is node.left: tmp = node.right else: break s = Solution() root = TreeNode.fromList([1, 2, 3, 4, 5, '#', 6, 7, '#', '#', '#', '#', 8]) s.connect(root)
sums = node.val + left + right else: maximum = max(maximum, left, node.val, left + node.val) sums = max(node.val, node.val + left) node = None return maximum #Binary Tree maximum path sum def maxPathSum(self, root): if root is None: return 0 if root.left is None and root.right is None: return root.val left = None right = None if root.left is not None: left = self.maxPathSum(root.left) if root.right is not None: right = self.maxPathSum(root.right) if left is None and right is not None: return max(root.val, root.val + right, right) elif left is not None and right is None: return max(root.val, root.val + left, left) return max(root.val, root.val + left, root.val + right, root.val + left + right, left, right) root = TreeNode.fromList([-2, -1]) s = Solution() print s.maxPathSum(root)