-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path07.cousinsInBinaryTree.cpp
33 lines (28 loc) · 1.06 KB
/
07.cousinsInBinaryTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int parent_x = -1, parent_y = -1, depth_x = -1, depth_y = -1, x, y;
void findDepthAndCheckParent(TreeNode* root, int depth) {
if(root == NULL) return;
if(root->left && root->left->val == x || root->right && root->right->val == x) parent_x = root->val;
if(root->left && root->left->val == y || root->right && root->right->val == y) parent_y = root->val;
if(root->val == x) depth_x = depth;
if(root->val == y) depth_y = depth;
findDepthAndCheckParent(root->left, depth + 1);
findDepthAndCheckParent(root->right, depth + 1);
}
bool isCousins(TreeNode* root, int x, int y) {
if(root == NULL) return false;
this->x = x, this->y = y;
findDepthAndCheckParent(root, 0);
return (depth_x == depth_y) && (parent_x != parent_y);
}
};