root whose node values are all distinct, and two different values a and b that are both present in the tree.
The root is at depth 0, and the children of a node at depth k are at depth k + 1. Two nodes are cousins when they are at the same depth but have different parents.
Return True if the nodes holding a and b are cousins, and False otherwise.root=tree_node([7, 3, 9, 5, None, None, 8]), a=5, b=8 → True
5 is a child of 3 and 8 is a child of 9. Both sit at depth 2 with different parents, so the answer is True.
root=tree_node([7, 3, 9, 5, 6]), a=5, b=6 → False
5 and 6 are at the same depth but share the parent 3: they are siblings, not cousins.
root=tree_node([7, 3, 9, 5]), a=5, b=9 → False
5 is at depth 2 while 9 is at depth 1.
Node.val ≤ 100, all values are distincta ≠ b, and both values are in the treeroot=tree_node([7, 3, 9, 5, None, None, 8]), a=5, b=8
True
Run checks the first 3 tests in your browser. Submit checks all 56 tests on the server, hidden ones included.