Check Binary-Tree Shapes
Given a binary tree with N nodes, determine whether it is full, complete, and perfect.
The nodes are identified by the integers from 0 to N - 1. The integer root is the root node, and left[i] and right[i] are the left and right children of node i. A value of -1 means that the corresponding child does not exist.
- A full binary tree gives every node either zero or two children.
- A complete binary tree fills every level except possibly the last, and the nodes on the last level occupy the leftmost positions.
- A perfect binary tree is full and has all leaves at the same depth.
Return one string containing three space-separated values in this order: the full result, the complete result, and the perfect result. Use YES when a property holds and NO otherwise.
The empty tree is considered full, complete, and perfect.
Example 1:
Input: N = 2, root = 0, left = [1, -1], right = [-1, -1]
Output: NO YES NO
Explanation: The root has only a left child, so the tree is not full or perfect. Its last-level node is in the leftmost position, so the tree is complete.
Example 2:
Input: N = 7, root = 3, left = [-1, 0, -1, 1, -1, 4, -1], right = [-1, 2, -1, 5, -1, 6, -1]
Output: YES YES YES
Explanation: Every internal node has two children, both levels below the root are filled, and all leaves have depth 2.
Now Your Turn!
Pick the correct output for the given inputInput: N = 5, root = 0, left = [1, 3, -1, -1, -1], right = [2, 4, -1, -1, -1]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 0 <= N <= 2 * 105
- left.length = right.length = N
- Every value in left and right is either -1 or an integer in the range [0, N - 1].
- If N = 0, root = -1 and both arrays are empty.
- If N > 0, root is in the range [0, N - 1].
- For N > 0, every node is reachable exactly once from root and the input is acyclic.