class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def binaryTreePaths(self, root): def inorder_traversal(node, path, paths): if not node: return if not node.left and not node.right: paths.append(path + str(node.val)) return inorder_traversal(node.left, path + str(node.val) + "->", paths) inorder_traversal(node.right, path + str(node.val) + "->", paths) paths = [] inorder_traversal(root, "", paths) return paths Give the space complexity of this in big O notation, give a single answer in big O
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def binaryTreePaths(self, root):
def inorder_traversal(node, path, paths):
if not node:
return
if not node.left and not node.right:
paths.append(path + str(node.val))
return
inorder_traversal(node.left, path + str(node.val) + "->", paths)
inorder_traversal(node.right, path + str(node.val) + "->", paths)
paths = []
inorder_traversal(root, "", paths)
return paths
Give the space complexity of this in big O notation, give a single answer in big O
Unlock instant AI solutions
Tap the button
to generate a solution