Loading...
Given the root of a binary tree whose node values are unique, and two different values x and y that both appear in it, return true when the node holding x and the node holding y are at the same depth but do not share a parent, and false otherwise.
The root is at depth 0, and the children of a node at depth k are at depth k + 1. Two nodes that share a parent are siblings; siblings are always at the same depth, so they are exactly the case the "different parent" rule excludes.
Your function receives root as a TreeNode. The node type is provided for you, with val, left and right fields.
The examples below write the tree as its level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how the input is displayed; the decoding is done for you.
Node.val ≤100x = y, and both appear in the treeThe node holding 4 is at depth 2 and the node holding 3 is at depth 1, so they are not at the same depth.
Both 4 and 5 sit at depth 2, and their parents are 2 and 3 respectively, so they are at the same depth with different parents.
Both 2 and 3 sit at depth 1, but they share the parent 1, so they are siblings.
Click "Run" to test with sample cases or "Submit" to run all tests.