SDE Path

Check if a Binary Tree Is Balanced

Medium

Check if a Binary Tree Is Balanced

Determine whether a binary tree is height-balanced.

Input format

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

Output format

Print true if the tree is balanced (heights of subtrees differ ≤ 1 everywhere), false otherwise.

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
72 56 50 86 88 43 45 N 40 57 62 N 21 N N N 32 N 35 16 77
Expected output
false
Example 2
Input
46 39 68 N N 99 95 70 49 N N 55 80 N 57 N N 65 N N N 1 73 N N N 96
Expected output
false