Exemplo n.º 1
0
		"""
		if not root :
			return 0
		self.res = []
		self.dfs(root,"")
		return (self.res)

	def dfs(self, root, s):
		# print(val)
		# if root:
		#
		if  not root.left and not root.right:
			# print(root.val)
			self.res.append(int(s+str(root.val)))
			return
		# if not root :
		# 	print(root.val)
		# 	self.res.append(int(s))
		# 	return
		else:
			if root.left:
				self.dfs(root.left,s+str(root.val))
			if root.right:
				self.dfs(root.right,s+str(root.val))
a = Solution()
treelist = [4,9,0,5,1]
treelist1= [0,2]
t = creatTree()
tree = t.list_to_tree(treelist,TreeNode,0)
print(a.sumNumbers(tree))
        self.helper(root)
        return self.res

    def helper(self, root):
        if not root:
            return
        self.res.append(root.val)
        self.helper(root.left)
        self.helper(root.right)

    def preorderTraversal1(self, root):
        res = []
        if not root:
            return res
        stack = []
        stack.append(root)
        while stack:
            node = stack.pop()
            res.append(node.val)
            if node.right:
                stack.append(node.right)
            if node.left:
                stack.append(node.left)
        return res


treelist = [1, "#", 2, 3, 4, 5]
tree = creatTree()
t = tree.list_to_tree(treelist, TreeNode, 0)
a = Solution()
print(a.preorderTraversal(t))
Exemplo n.º 3
0
	def find_all_sum(self, root,temp):
		# print(root.val)
		if not root:
			return

		if  not root.right and not root.left:
			# print(root.val)
			self.all_sum.append(temp+root.val)
			# print(self.all_sum)
			return
		# elif (not root.right and  root.left) or (not root.left and root.right):
		# 	return
		else:
			print(root.val)
			self.find_all_sum(root.left,temp+root.val)
			# print("s",root.val)

			self.find_all_sum(root.right,temp+root.val)




treelist = [1,2,3,'#',4,5]
treelist1 = [5,4,8,11,"#",13,4,7,2]
tree_obj = creatTree()
tree = tree_obj.list_to_tree(treelist1,TreeNode,0)



a = Solution()
print(a.hasPathSum(tree,0))