-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lowest Common Ancestor of a Binary Tree.cpp
59 lines (58 loc) · 1.6 KB
/
Lowest Common Ancestor of a Binary Tree.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
/**
* 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:
stack<TreeNode*> stap;
stack<TreeNode*> staq;
bool PreOrder(TreeNode* root, TreeNode* Node, stack<TreeNode*>& sta, int *len)//入栈
{
if(root==NULL)
return false;
sta.push(root);
(*len)++;
if(root == Node)
return true;
if(PreOrder(root->left, Node, sta, len))
return true;
if(root != NULL && PreOrder(root->right, Node, sta, len))
return true;
sta.pop();
(*len)--;
return false;
}
TreeNode* CommonNode(stack<TreeNode*> stap, stack<TreeNode*> staq, int plen, int qlen)//找两个栈的公共结点
{
if(plen < qlen)
return CommonNode(staq, stap, qlen, plen);
while(plen > qlen)
{
stap.pop();
plen--;
}
while(!stap.empty() && !staq.empty())
{
if(stap.top() == staq.top())
return stap.top();
stap.pop();
staq.pop();
}
return NULL;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==NULL||p==NULL||q==NULL)
return NULL;
TreeNode* pNode = root;
int plen = 0, qlen = 0;
PreOrder(pNode, p, stap, &plen);
pNode = root;
PreOrder(pNode, q, staq, &qlen);
return CommonNode(stap, staq, plen, qlen);
}
};