All Tree Nodes at the Same Depth
Problem statement
Return whether every node in the binary tree has the same depth. Include the root, internal nodes and leaves. The root has depth 0; each child has its parent's depth plus 1. Return true for an empty tree. Node values do not affect the answer. Leave the tree unchanged.
This exercise uses the reported word nodes literally. A root with any child already contains nodes at different depths.
Function
allNodesSameDepth(root: TreeNode) → booleanExamples
Example 1
root = [7]return = trueThe only node is the root, at depth 0.
Example 2
root = [1,2,3]return = falseThe root has depth 0 and both children have depth 1. Equal leaf depths do not make all node depths equal.
Constraints
0 <= serialized tree length <= 400.-1000 <= every non-null node value <= 1000.- The input is a valid connected acyclic binary tree serialized in level order, with null for missing children; [] denotes an empty tree.