#C1146. Deepest Left Leaf Node

    ID: 40778 Type: Default 1000ms 256MiB

Deepest Left Leaf Node

Deepest Left Leaf Node

Given a binary tree, your task is to find the value of the deepest left leaf node. A left leaf is defined as a node that:

  • Is a leaf node (i.e. it has no children).
  • Is the left child of its parent.

If there is no left leaf node in the tree, print -1.

The binary tree is given in a single line in level-order where each node is separated by a space and missing nodes are represented by N. For instance, the binary tree illustrated below:

1 2 3 4 5 N N 7 N N N

represents the binary tree:

       1
      / \
     2   3
    / \
   4   5
  /
 7

In this example, the deepest left leaf is the node with value 7 at depth 3.

Note: You may find it useful to think of the tree as a collection of nodes where the root is at depth 0 and each level increases the depth by 1. In LaTeX, you can depict the depth increment as follows: $$depth(node) = depth(parent) + 1.$$

inputFormat

The input is provided via standard input (stdin) as a single line containing space-separated tokens that represent the binary tree in level-order. The token N denotes a null (missing) child.

For example:

1 2 3 4 5 N N 7 N N N

outputFormat

Output a single integer to standard output (stdout) representing the value of the deepest left leaf node in the binary tree. If no left leaf exists, output -1.

## sample
1 2 3 4 5 N N 7 N N N
7