Check height balance · Compiler

Check height balance

Check whether every node has left and right subtree heights differing by at most one. Measure subtree height in nodes: an empty subtree has height 0 and a leaf has height 1. The empty tree is balanced.

Input
Read n (0 <= n <= 100), then n rows of value left right. Rows give nodes in ID order from 1 to n. Each value is an integer in [-1000, 1000]. Each child link is a node ID or 0 for a missing child. For n > 0, the links form one binary tree rooted at ID 1.
Output
Print YES if the tree is height-balanced, or NO otherwise.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…