#C1035. Modify the Binary Tree Values
Modify the Binary Tree Values
Modify the Binary Tree Values
You are given a binary tree. Your task is to modify its nodes' values using a level-order traversal. For each node:
- If the current node's value is even, replace it with \(\frac{value}{2}\).
- If the current node's value is odd, replace it with \(2 \times value\).
The tree is provided as a space-separated list representing its level-order traversal. Use the special string null
to denote the absence of a node. After processing, output the modified tree's level-order traversal as space-separated integers.
Example 1:
Input: 6 4 7 3 5 1 8</p>Explanation: Original tree: 6 /
4 7 / \ /
3 5 1 8Processing: 6 is even (\to 6/2=3); 4 is even (\to 4/2=2); 7 is odd (\to 72=14); 3 is odd (\to 32=6); 5 is odd (\to 52=10); 1 is odd (\to 12=2); 8 is even (\to 8/2=4);
Output: 3 2 14 6 10 2 4
Example 2:
Input: 4 6 8 10</p>Explanation: Original tree: 4 /
6 8 / 10Processing: 4 is even (\to 2); 6 is even (\to 3); 8 is even (\to 4); 10 is even (\to 5);
Output: 2 3 4 5
inputFormat
The input is a single line read from stdin
that contains a space-separated list of tokens representing the binary tree in level order. The token null
represents a missing node. You can assume that the tree is non-empty.
For example: 6 4 7 3 5 1 8
outputFormat
Output the level-order traversal of the modified binary tree as a single line of space-separated integers to stdout
.
6 4 7 3 5 1 8
3 2 14 6 10 2 4