Skip to content

Commit 2b6d1a4

Browse files
committed
0993 updated.
1 parent 182f7c6 commit 2b6d1a4

File tree

3 files changed

+123
-3
lines changed

3 files changed

+123
-3
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.13)
2-
project(cpp_0993)
2+
project(A)
33

44
set(CMAKE_CXX_STANDARD 11)
55

6-
add_executable(cpp_0993 main.cpp)
6+
add_executable(A main2.cpp)
Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,67 @@
1+
/// Source : https://leetcode.com/problems/cousins-in-binary-tree/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-16
4+
15
#include <iostream>
6+
#include <cassert>
7+
8+
using namespace std;
9+
10+
11+
/// Two-Pass DFS
12+
/// Time Complexity: O(n)
13+
/// Space Complexity: O(h)
14+
15+
/// Definition for a binary tree node.
16+
struct TreeNode {
17+
int val;
18+
TreeNode *left;
19+
TreeNode *right;
20+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
21+
};
22+
23+
class Solution {
24+
25+
public:
26+
bool isCousins(TreeNode* root, int x, int y) {
27+
28+
if(!root || root->val == x || root->val == y)
29+
return false;
30+
31+
pair<int, int> p1; // depth, parent
32+
if(!dfs(root, x, 0, p1)) return false;
33+
34+
pair<int, int> p2;
35+
if(!dfs(root, y, 0, p2)) return false;
36+
37+
return p1.first == p2.first && p1.second != p2.second;
38+
}
39+
40+
private:
41+
bool dfs(TreeNode* node, int x, int d, pair<int, int>& p){
42+
43+
if(node->left && node->left->val == x){
44+
p = make_pair(d + 1, node->val);
45+
return true;
46+
}
47+
48+
if(node->right && node->right->val == x){
49+
p = make_pair(d + 1, node->val);
50+
return true;
51+
}
52+
53+
if(node->left && dfs(node->left, x, d + 1, p))
54+
return true;
55+
56+
if(node->right && dfs(node->right, x, d + 1, p))
57+
return true;
58+
59+
return false;
60+
}
61+
};
62+
263

364
int main() {
4-
std::cout << "Hello, World!" << std::endl;
65+
566
return 0;
667
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/// Source : https://leetcode.com/problems/cousins-in-binary-tree/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-18
4+
5+
#include <iostream>
6+
#include <cassert>
7+
#include <unordered_map>
8+
9+
using namespace std;
10+
11+
12+
/// One-Pass DFS
13+
/// Using HashMap to record the result :-)
14+
///
15+
/// Time Complexity: O(n)
16+
/// Space Complexity: O(n)
17+
18+
/// Definition for a binary tree node.
19+
struct TreeNode {
20+
int val;
21+
TreeNode *left;
22+
TreeNode *right;
23+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
24+
};
25+
26+
class Solution {
27+
28+
public:
29+
bool isCousins(TreeNode* root, int x, int y) {
30+
31+
if(!root || root->val == x || root->val == y)
32+
return false;
33+
34+
unordered_map<int, pair<int, int>> record; // depth, parent
35+
dfs(root, 0, record);
36+
37+
return record[x].first == record[y].first && record[x].second != record[y].second;
38+
}
39+
40+
private:
41+
void dfs(TreeNode* node, int d, unordered_map<int, pair<int, int>>& record){
42+
43+
if(node->left) {
44+
record[node->left->val] = make_pair(d + 1, node->val);
45+
dfs(node->left, d + 1, record);
46+
}
47+
48+
if(node->right){
49+
record[node->right->val] = make_pair(d + 1, node->val);
50+
dfs(node->right, d + 1, record);
51+
}
52+
}
53+
};
54+
55+
56+
int main() {
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)