-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowest_Common_Ancestor_of_a_Binary_Tree.cpp
More file actions
48 lines (46 loc) · 1.18 KB
/
Lowest_Common_Ancestor_of_a_Binary_Tree.cpp
File metadata and controls
48 lines (46 loc) · 1.18 KB
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
# number : 236
/**
* 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:
deque<TreeNode*> v1, v2;
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == nullptr)
return nullptr;
dfs(root, p, v1);
dfs(root, q, v2);
TreeNode* result = v1.front();
while (!v1.empty() && !v2.empty()) {
int t1 = v1.front()->val;
int t2 = v2.front()->val;
if (t1 == t2) {
result = v1.front();
v1.pop_front();
v2.pop_front();
}
else
break;
}
return result;
}
bool dfs(TreeNode *root, TreeNode *node, deque<TreeNode*>& v) {
if (!root)
return false;
v.push_back(root);
if (root == node)
return true;
if (dfs(root->left, node, v))
return true;
if (dfs(root->right, node, v))
return true;
v.pop_back();
return false;
}
};