forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
59 lines (43 loc) · 1.25 KB
/
main2.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// Source : https://leetcode.com/problems/cousins-in-binary-tree/
/// Author : liuyubobobo
/// Time : 2019-02-18
#include <iostream>
#include <cassert>
#include <unordered_map>
using namespace std;
/// One-Pass DFS
/// Using HashMap to record the result :-)
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
/// 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:
bool isCousins(TreeNode* root, int x, int y) {
if(!root || root->val == x || root->val == y)
return false;
unordered_map<int, pair<int, int>> record; // depth, parent
dfs(root, 0, record);
return record[x].first == record[y].first && record[x].second != record[y].second;
}
private:
void dfs(TreeNode* node, int d, unordered_map<int, pair<int, int>>& record){
if(node->left) {
record[node->left->val] = make_pair(d + 1, node->val);
dfs(node->left, d + 1, record);
}
if(node->right){
record[node->right->val] = make_pair(d + 1, node->val);
dfs(node->right, d + 1, record);
}
}
};
int main() {
return 0;
}