题目
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 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).”
6
/ \
2 8
/ \ / \
0 4 7 9
/ \
3 5
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
思路
[算法系列之三十一]最近公共祖先(LCA)
代码
#include <iostream>
#include <vector>
using namespace std;
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) {
if(root == nullptr || p == nullptr || q == nullptr){
return nullptr;
}
return helper(root,p,q);
}
private:
TreeNode* helper(TreeNode* root,TreeNode* p,TreeNode* q){
if(root == nullptr || p == nullptr || q == nullptr){
return nullptr;
}
int pVal = p->val;
int qVal = q->val;
int rootVal = root->val;
if((pVal <= rootVal && qVal >= rootVal) || (pVal >= rootVal && qVal <= rootVal)){
return root;
}
if(pVal < rootVal && qVal < rootVal){
return helper(root->left,p,q);
}
if(pVal > rootVal && qVal > rootVal){
return helper(root->right,p,q);
}
}
};
int main(){
Solution s;
TreeNode* root = new TreeNode(6);
TreeNode* node1 = new TreeNode(0);
TreeNode* node2 = new TreeNode(9);
TreeNode* node3 = new TreeNode(2);
TreeNode* node4 = new TreeNode(3);
TreeNode* node5 = new TreeNode(4);
TreeNode* node6 = new TreeNode(5);
TreeNode* node7 = new TreeNode(7);
TreeNode* node8 = new TreeNode(8);
root->left = node3;
root->right = node8;
node3->left = node1;
node3->right = node5;
node5->left = node4;
node5->right = node6;
node8->left = node7;
node8->right = node2;
TreeNode* node = s.lowestCommonAncestor(root,node3,node4);
if(node != nullptr){
cout<<node->val<<endl;
}
else{
cout<<"nullptr"<<endl;
}
return 0;
}
运行时间
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
1123.Lowest_Common_Ancestor_of_Deepest_Leaves最深叶节点的最近公共祖先【LeetCo
用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现...
JVM 基础 JAVA 并发 JVM 性能调优 LeetCode 算法 .......
原创:leetcode 107. 二叉树的层次遍历 II【队列】* Definition for a binary tree node.
原创:leetcode 111. 二叉树的最小深度记住:最小深度和最大深度方法不同。* Definition for a binary tree node.in
My Solutions to Leetcode Database problems. 我的 Leetcode 数据.zip
Leetcode 题解.pdf
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
LeetCode 后端.zip
Leetcode101.zip
刷leetcode总结.md
Recording personal Java, Python, JavaScript solutions for Leetcode problems. 记录个人 Java, Python, JavaScript 的Leetcode题解.zip
原创:leetcode 5. 最长回文子串//寻找以i-1,i为中点偶数长度的回文//寻找以i为中心的奇数长度的回文。
My Solutions to Leetcode problems. All solutions support C
原创:leetcode 22.括号生成【回溯】对待这种问题,千万别暴力搜索,那样太笨了。但是这个方法是最容易理解的//回溯法 (后面的括号) 不可以大于 (前面
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
LeetCode674. 最长连续递增序列674. 最长连续递增序列解题思路:记录每次递增序列的长度,max存储最大长度// 递增序列更新最大长度} else
LeetCode888. 公平的糖果棒交换888. 公平的糖果棒交换解题思路:哈希表法第一次遍历 A\B 计算二者的糖果差值 diff,同时以 B糖果值建立哈希