Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Example 1:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.
Example 2:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
Example 3:
Input: root = [2,1], p = 2, q = 1
Output: 2
Constraints:
- The number of nodes in the tree is in the range
[2, 105]
. -109 <= Node.val <= 109
- All
Node.val
are unique. p != q
-
p
andq
will exist in the BST.
我的答案:
/**
* 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:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
vector ancester1, ancester2;
SearchNode(root, p, ancester1);
SearchNode(root, q, ancester2);
// cout << ancester1[0]->val << " - " << ancester2[0]->val << endl;
int i = 1;
for (; ival << " - " << ancester2[i]->val << endl;
if (ancester1[i] != ancester2[i]) {
return ancester1[i-1];
}
}
return ancester1[i-1]; // mark2
}
void SearchNode(TreeNode* root, TreeNode* node, vector& ancester) {
if (node->val == root->val) {
ancester.push_back(root); // mark1
return;
}
ancester.push_back(root);
if (node->val < root->val) {
SearchNode(root->left, node, ancester);
}
else {
SearchNode(root->right, node, ancester);
}
}
};
Runtime: 28 ms, faster than 90.94% of C++ online submissions for Lowest Common Ancestor of a Binary Search Tree.
Memory Usage: 24 MB, less than 24.08% of C++ online submissions for Lowest Common Ancestor of a Binary Search Tree.
感觉这么做下来,不是E的题。
- 首先需要想到是binary search tree,所以存储每个node的ancestor比较容易了,正向搜索也只有唯一解
- 其次一个坑是mark1,需要把自己也放到ancestor里面
- 最后mark2的地方也需要注意,到这一步说明前面的ancestor匹配都对,只是size不同出来,所以就需要return ancester1[i-1];,但注意不是return ancester1[i];,因为出来的i已经++过了
不过结合binary search tree的性质,其实有更简单的方法
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/solution/
官方答案
默写recursive答案:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode* ans = root;
if (ans->val > p->val and ans->val > q->val) {
ans = lowestCommonAncestor(root->left, p, q);
}
else if (ans->val < p->val and ans->val < q->val) {
ans = lowestCommonAncestor(root->right, p, q);
}
// else {
// }
return ans;
}
};
Runtime: 36 ms, faster than 38.67% of C++ online submissions for Lowest Common Ancestor of a Binary Search Tree.
Memory Usage: 23.9 MB, less than 24.08% of C++ online submissions for Lowest Common Ancestor of a Binary Search Tree.
默写iterative答案:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode* ans = root;
while (ans != p and ans != q) {
if (ans->val > p->val and ans->val > q->val) {
ans = ans->left;
}
else if (ans->val < p->val and ans->val < q->val) {
ans = ans->right;
}
else {
break;
}
}
return ans;
}
};
Runtime: 24 ms, faster than 98.52% of C++ online submissions for Lowest Common Ancestor of a Binary Search Tree.
Memory Usage: 23.9 MB, less than 24.08% of C++ online submissions for Lowest Common Ancestor of a Binary Search Tree.