-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
LowestCommonAncestorOfABinaryTree.cpp
102 lines (88 loc) · 3.38 KB
/
LowestCommonAncestorOfABinaryTree.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
92
93
94
95
96
97
98
99
100
101
102
// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
// Author : Hao Chen
// Date : 2015-07-17
/**********************************************************************************
*
* Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the
* tree.
*
* According to the definition of LCA on Wikipedia: “The lowest common ancestor is
* defined between two nodes v and w as the lowest node in T that has both v and w as
* descendants (where we allow a node to be a descendant of itself).”
*
* _______3______
* / \
* ___5__ ___1__
* / \ / \
* 6 _2 0 8
* / \
* 7 4
*
* For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example
* is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according
* to the LCA definition.
*
*
*
**********************************************************************************/
/**
* 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 findPath(TreeNode* root, TreeNode* p, vector<TreeNode*>& path) {
if (root==NULL) return false;
if (root == p) {
path.push_back(p);
return true;
}
path.push_back(root);
if (findPath(root->left, p, path)) return true;
if (findPath(root->right, p, path)) return true;
path.pop_back();
return false;
}
//Ordinary way, find the path and comapre the path.
TreeNode* lowestCommonAncestor01(TreeNode* root, TreeNode* p, TreeNode* q) {
vector<TreeNode*> path1, path2;
if (!findPath(root, p, path1)) return NULL;
if (!findPath(root, q, path2)) return NULL;
int len = path1.size() < path2.size() ? path1.size() : path2.size();
TreeNode* result = root;
for(int i=0; i<len; i++) {
if (path1[i] != path2[i]) {
return result;
}
result = path1[i];
}
return result;
}
//Actually, we can do the recursive search in one time
TreeNode* lowestCommonAncestor02(TreeNode* root, TreeNode* p, TreeNode* q) {
//return if found or not found, return NULL if not found
if (root==NULL || root == p || root == q) return root;
//find the LCA in left tree
TreeNode* left = lowestCommonAncestor02(root->left, p, q);
//find the LCA in right tree
TreeNode* right = lowestCommonAncestor02(root->right, p, q);
//left==NULL means both `p` and `q` are not found in left tree.
if (left==NULL) return right;
//right==NULL means both `p` and `q` are not found in right tree.
if (right==NULL) return left;
// left!=NULL && right !=NULL, which means `p` & `q` are seperated in left and right tree.
return root;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
srand(time(0));
if (random()%2) {
return lowestCommonAncestor02(root, p, q);
}
return lowestCommonAncestor01(root, p, q);
}
};