Skip to content

Commit afd3a24

Browse files
committed
New Problem "Lowest Common Ancestor of a Binary Tree"
1 parent ec87683 commit afd3a24

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ LeetCode
77

88
| # | Title | Solution | Difficulty |
99
|---| ----- | -------- | ---------- |
10-
|218|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| [C++](./algorithms/lowestCommonAncestorOfABinarySearchTree/LowestCommonAncestorOfABinarySearchTree.cpp)|Easy|
10+
|220|[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| [C++](./algorithms/lowestCommonAncestorOfABinaryTree/LowestCommonAncestorOfABinaryTree.cpp)|Easy|
11+
|219|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| [C++](./algorithms/lowestCommonAncestorOfABinarySearchTree/LowestCommonAncestorOfABinarySearchTree.cpp)|Easy|
1112
|218|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)| [C++](./algorithms/palindromeLinkedList/PalindromeLinkedList.cpp)|Easy|
1213
|217|[Number of Digit One](https://leetcode.com/problems/number-of-digit-one/)| [C++](./algorithms/numberOfDigitOne/NumberOfDigitOne.cpp)|Medium|
1314
|216|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)| [C++](./algorithms/implementQueueUsingStacks/ImplementQueueUsingStacks.cpp)|Easy|
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
2+
// Author : Hao Chen
3+
// Date : 2015-07-17
4+
5+
/**********************************************************************************
6+
*
7+
* Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the
8+
* tree.
9+
*
10+
* According to the definition of LCA on Wikipedia: “The lowest common ancestor is
11+
* defined between two nodes v and w as the lowest node in T that has both v and w as
12+
* descendants (where we allow a node to be a descendant of itself).”
13+
*
14+
* _______3______
15+
* / \
16+
* ___5__ ___1__
17+
* / \ / \
18+
* 6 _2 0 8
19+
* / \
20+
* 7 4
21+
*
22+
* For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example
23+
* is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according
24+
* to the LCA definition.
25+
*
26+
*
27+
*
28+
**********************************************************************************/
29+
30+
31+
/**
32+
* Definition for a binary tree node.
33+
* struct TreeNode {
34+
* int val;
35+
* TreeNode *left;
36+
* TreeNode *right;
37+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
38+
* };
39+
*/
40+
class Solution {
41+
public:
42+
bool findPath(TreeNode* root, TreeNode* p, vector<TreeNode*>& path) {
43+
if (root==NULL) return false;
44+
if (root == p) {
45+
path.push_back(p);
46+
return true;
47+
}
48+
49+
path.push_back(root);
50+
if (findPath(root->left, p, path)) return true;
51+
if (findPath(root->right, p, path)) return true;
52+
path.pop_back();
53+
54+
return false;
55+
}
56+
57+
//Ordinary way, find the path and comapre the path.
58+
TreeNode* lowestCommonAncestor01(TreeNode* root, TreeNode* p, TreeNode* q) {
59+
60+
vector<TreeNode*> path1, path2;
61+
62+
if (!findPath(root, p, path1)) return NULL;
63+
if (!findPath(root, q, path2)) return NULL;
64+
65+
int len = path1.size() < path2.size() ? path1.size() : path2.size();
66+
TreeNode* result = root;
67+
for(int i=0; i<len; i++) {
68+
if (path1[i] != path2[i]) {
69+
return result;
70+
}
71+
result = path1[i];
72+
}
73+
return result;
74+
}
75+
76+
//Actually, we can do the recursive search in one time
77+
TreeNode* lowestCommonAncestor02(TreeNode* root, TreeNode* p, TreeNode* q) {
78+
79+
//return if found or not found, return NULL if not found
80+
if (root==NULL || root == p || root == q) return root;
81+
82+
//find the LCA in left tree
83+
TreeNode* left = lowestCommonAncestor02(root->left, p, q);
84+
//find the LCA in right tree
85+
TreeNode* right = lowestCommonAncestor02(root->right, p, q);
86+
87+
//left==NULL means both `p` and `q` are not found in left tree.
88+
if (left==NULL) return right;
89+
//right==NULL means both `p` and `q` are not found in right tree.
90+
if (right==NULL) return left;
91+
// left!=NULL && right !=NULL, which means `p` & `q` are seperated in left and right tree.
92+
return root;
93+
}
94+
95+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
96+
srand(time(0));
97+
if (random()%2) {
98+
return lowestCommonAncestor02(root, p, q);
99+
}
100+
return lowestCommonAncestor01(root, p, q);
101+
}
102+
};

0 commit comments

Comments
 (0)