defprintPaths(node, path): if node == None: return
# add node to the path path.append(node.val)
if node.left == Noneand node.right == None: # this is leaf node print("path : ", end="") for n in path: print(f"{n} ", end="") print() else: # search left subtree printPaths(node.left, path)
# search right subtree printPaths(node.right, path)
# remove node from path path.pop() printPaths(root, list())
he root of a binary tree, flatten the tree into a “linked list”:
The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
The “linked list” should be in the same order as a pre-order traversal of the binary tree.
Constraints:
The number of nodes in the tree is in the range [0, 2000].
-100 <= Node.val <= 100
Follow up: Can you flatten the tree in-place (with O(1) extra space)?