Page tree
Skip to end of metadata
Go to start of metadata
class Solution:
    def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
        result = []
        if not root:
            return result
        
        def dfs(node: TreeNode, path: List[int]):
            if not node:
                return
            if not node.left and not node.right:
                path.append(node.val)
                total = 0
                for i in path:
                    total += i
                if total == sum:
                    result.append(path)

            if node.left:
                dfs(node.left, path + [node.val])
            if node.right:
                dfs(node.right, path + [node.val])

        dfs(root, [])
        return result
class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        result = []
        def dfs(node):
            if not node:
                return
            dfs(node.left)
            result.append(node.val)
            dfs(node.right)

        dfs(root)
        return result
class Solution:
    def finalPrices(self, prices: List[int]) -> List[int]:
        result = []
        for i in range(len(prices)):
            discount = 0
            for j in range(i + 1, len(prices)):
                if prices[j] <= prices[i]:
                    discount = prices[j]
                    break
            result.append(prices[i] - discount)
        return result
  • No labels