#C8079. Binary Tree Paths
Binary Tree Paths
Binary Tree Paths
Given a binary tree, return all root-to-leaf paths as individual strings. The tree is represented in level order traversal where each value is separated by spaces and a missing (null) node is denoted by N
. A path is constructed by concatenating node values from the root to a leaf, with the arrow symbol ->
between consecutive values.
For example, the tree represented by the input:
1 2 3 N 5 N N
corresponds to the following binary tree:
1 / \ 2 3 \ 5
The output in this case would be:
1->2->5 1->3
Your task is to read the tree from standard input, construct it, and then output all the root-to-leaf paths, each on a new line.
inputFormat
Input is read from standard input as a single line containing space-separated tokens representing the binary tree in level order traversal. Each token is either an integer (node value) or N
to represent a null (missing) node. For example:
1 2 3 N 5 N N
outputFormat
Output the list of root-to-leaf paths. Each path should be printed on a separate line. Nodes in a path are connected by the string ->
. If the tree is empty, do not output anything.
1 2 3 N 5 N N
1->2->5
1->3
</p>