Given the root
of a binary tree with unique values and the values of two different nodes of the tree x
and y
, return true
if the nodes corresponding to the values x
and y
in the tree are cousins*, or false
otherwise.*
Two nodes of a binary tree are cousins if they have the same depth with different parents.
Note that in a binary tree, the root node is at the depth 0
, and children of each depth k
node are at the depth k + 1
.
Example 1:
Input: root = [1,2,3,4], x = 4, y = 3
Output: false
Example 2:
Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
Output: true
We can solve this problems in multiple approach:
Approcah 1:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
struct node {
TreeNode* root ;
TreeNode* parent ;
int val ;
node( TreeNode* a, TreeNode* b, int x ) {
root = a ;
parent = b ;
val = x ;
}
};
public:
bool isCousins(TreeNode* root, int x, int y) {
stack< node > Stk ;
Stk.push( node( root, nullptr, 0 ) ) ;
node a( nullptr, nullptr, 0 ), b( nullptr, nullptr, 0 ) ;
while( !Stk.empty() )
{
node temp = Stk.top() ;
Stk.pop() ;
if( temp.root -> val == x ) a = node( temp.root, temp.parent, temp.val ) ;
if( temp.root -> val == y ) b = node( temp.root, temp.parent, temp.val ) ;
if( temp.root -> left ) Stk.push( node( temp.root -> left , temp.root, temp.val + 1 ) ) ;
if( temp.root -> right ) Stk.push( node( temp.root -> right , temp.root, temp.val + 1 ) ) ;
}
if( a.val == b.val && a.parent != b.parent ) return true ;
else return false ;
}
};
Approcah 2:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
void Fun( TreeNode* root, int d, int &dx,int &dy,int &px,int &py, int x, int y )
{
if( root == NULL ) return ;
if( root -> left )
{
if( root -> left -> val == x )
{
dx = d + 1 ;
px = root -> val ;
}
else if( root -> left -> val == y )
{
dy = d + 1 ;
py = root -> val ;
}
}
if( root -> right )
{
if( root -> right -> val == x )
{
dx = d + 1 ;
px = root -> val ;
}
else if( root -> right -> val == y )
{
dy = d + 1 ;
py = root -> val ;
}
}
if( px != -1 && py != -1 ) return ;
Fun( root -> left , d + 1, dx, dy, px, py, x, y ) ;
Fun( root -> right , d + 1, dx, dy, px, py, x, y ) ;
}
public:
bool isCousins(TreeNode* root, int x, int y)
{
int dx = 0 ;
int dy = 0 ;
int px = -1 ;
int py = -1 ;
Fun( root, 0, dx, dy, px, py, x, y ) ;
return ( ( dx == dy ) && ( px != py ) ) ;
}
};
Approcah 3:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isCousins(TreeNode* root, int x, int y)
{
int level[ 105 ] = {0} ;
stack< pair< TreeNode*, int > > Stk ;
if( root != NULL ) Stk.push( { root, 0 } ) ;
while( !Stk.empty() )
{
pair< TreeNode*, int > temp = Stk.top() ;
level[ temp.first -> val ] = temp.second ;
Stk.pop() ;
if( temp.first -> left != NULL && temp.first -> right != NULL )
{
if( temp.first -> left -> val == x && temp.first -> right -> val == y ) return false ;
if( temp.first -> left -> val == y && temp.first -> right -> val == x ) return false ;
}
if( temp.first -> left != NULL ) Stk.push( { temp.first -> left, temp.second + 1 } ) ;
if( temp.first -> right != NULL ) Stk.push( { temp.first -> right, temp.second + 1 } ) ;
}
if( level[ x ] == level[ y ] ) return true ;
else return false ;
}
};