forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
65 lines (48 loc) · 1.36 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
60
61
62
63
64
65
/// Source : https://leetcode.com/problems/flip-equivalent-binary-trees/
/// Author : liuyubobobo
/// Time : 2018-12-01
#include <iostream>
#include <vector>
using namespace std;
/// Canonical Traversal
/// Time Complexity: O(N1 + N2)
/// Space Complexity: O(N1 + N2)
/// 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 flipEquiv(TreeNode* root1, TreeNode* root2) {
vector<int> vec1, vec2;
dfs(root1, vec1);
// for(TreeNode* node: vec1)
// cout << (node ? node->val : -1) << " ";
// cout << endl;
dfs(root2, vec2);
// for(TreeNode* node: vec2)
// cout << (node ? node->val : -1) << " ";
// cout << endl;
return vec1 == vec2;
}
private:
void dfs(TreeNode* node, vector<int>& vec){
vec.push_back(node ? node->val : -1);
if(!node)
return;
int L = node->left ? node->left->val : -1;
int R = node->right ? node->right->val : -1;
if(L <= R)
dfs(node->left, vec), dfs(node->right, vec);
else
dfs(node->right, vec), dfs(node->left, vec);
}
};
int main() {
cout << Solution().flipEquiv(NULL, new TreeNode(1)) << endl;
// False
return 0;
}