-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path236.cpp
91 lines (83 loc) · 2.64 KB
/
236.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//参考113. Path Sum II,将前序遍历得到的路径放入vector里,回溯问题
//对比两个vector在第几位开始不同
//没搞懂哪错了,和下面几乎一样啊
/*class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
vector<TreeNode *> v_path_p;
vector<TreeNode *> v_path_q;
vector<TreeNode *> path;
preorderTraversal(root, p, path, v_path_p);
//path.clear();
preorderTraversal(root, q, path, v_path_q);
int i = 0;
for (; ;i++) {
if (v_path_p[i] != v_path_q[i]) {
break;
}
}
return v_path_p[i - 1];
}
private:
void preorderTraversal(TreeNode *root, TreeNode* aim, vector<TreeNode *> &path, vector<TreeNode *> &v_path) {
if (!root) {
return;
}
if (!v_path.empty()) {//如果找全了
return;
}
path.push_back(root);
if (root == aim) {
v_path = path;
}
preorderTraversal(root->left, aim, path, v_path);
preorderTraversal(root->right, aim, path, v_path);
path.pop_back();
}
};*/
//前序遍历节点, 先找到p,q. 同时记录下从root到该几点的路径。之后比较路径,最后一个相同的节点便是LCA.
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
vector<TreeNode *> v_path_p;
vector<TreeNode *> v_path_q;
vector<TreeNode *> path;
preorderTraversal(root, p, q, path, v_path_p, v_path_q);
int i = 0;
for (; ;i++) {
if (v_path_p[i] != v_path_q[i]) {
break;
}
}
return v_path_q[i - 1];
}
private:
void preorderTraversal(TreeNode *root, TreeNode* p,TreeNode* q,
vector<TreeNode *> &path,vector<TreeNode *> &v_path_p,vector<TreeNode *> &v_path_q) {
if (!root) {
return;
}
if (!v_path_p.empty() && !v_path_q.empty()) {//如果找全了
return;
}
path.push_back(root);
if (root == p) {
v_path_p = path;
}
if (root == q) {
v_path_q = path;
}
preorderTraversal(root->left, p, q, path, v_path_p, v_path_q);
preorderTraversal(root->right, p, q, path, v_path_p, v_path_q);
path.pop_back();
}
};