SDE Path

Binary Tree Preorder Traversal

Easy

Binary Tree Preorder Traversal

Traverse a binary tree in preorder (root, left, right), iteratively and recursively.

Input format

The first line contains level-order tokens, where N denotes a null node.

Output format

Print the preorder traversal of the tree, space-separated.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
51 29 N 17 22 84
Expected output
51 29 17 84 22
Example 2
Input
82 99 4
Expected output
82 99 4