Cousins in Binary Tree
Last updated
Last updated
In a binary tree, the root node is at depth0
, and children of each depthk
node are at depthk+1
.
Two nodes of a binary tree are_cousins_if they have the same depth, but have different parents.
We are given theroot
of a binary tree with unique values, and the valuesx
andy
of two different nodes in the tree.
Return true
if and only if the nodes corresponding to the valuesx
andy
are cousins.
Example 1:
Example 2:
Example 3:
直接暴力做吧,依据:深度一样,parent不一样,分别存一下吧