【LeetCode 235】Lowest Common Ancestor of a Binary Search Tree
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__
/ \ / \
_4
/ \
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.
思路:
利用BST的性质,若待查找两个结点的数值都小于当前结点则向左边查找(若有一个等于当前结点则当前结点就是其最小的公共结点,结束查找),反之向右边查找(同理),若一个大于一个小于,则当前结点就是其最小的公共结点,结束查找。
C++:
/**
* 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 {
private:
TreeNode *l, *r, *ret;
public: void rec(TreeNode *root)
{
int v = root->val;
if(l->val <= v && r->val <= v)
{
if(v == l->val || v == r->val)
{
ret = root;
return ;
} if(root->left != )
rec(root->left);
}
else if(l->val < v && v < r->val)
{
ret = root;
return ;
}
else if(l->val >= v && r->val >= v)
{
if(v == l->val || v == r->val)
{
ret = root;
return ;
} if(root->right != )
rec(root->right);
}
} TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == )
return ; if(p->val < q->val){
l = p;
r = q;
}
else{
l = q;
r = p;
} ret = ; rec(root); return ret;
}
};
【LeetCode 235】Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先
235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowes ...
- 【LeetCode 236】Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- LeetCode OJ:Lowest Common Ancestor of a Binary Search Tree(最浅的公共祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree
解法一:递归 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (root == NULL | ...
- LeetCode算法题-Lowest Common Ancestor of a Binary Search Tree
这是悦乐书的第197次更新,第203篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第59题(顺位题号是235).给定二叉搜索树(BST),找到BST中两个给定节点的最低共 ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)
Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- LeetCode_235. Lowest Common Ancestor of a Binary Search Tree
235. Lowest Common Ancestor of a Binary Search Tree Easy Given a binary search tree (BST), find the ...
随机推荐
- Netty4.x中文教程系列(一) 目录及概述
Netty4.x中文教程系列(一)目录及概述 Netty 提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. Netty是一个NIO客户端 服务端框架 ...
- storage size of ‘oldact’ isn’t known
#include <signal.h> int main(){struct sigaction act, oldact;return 0;} dies with the message t ...
- ActiveXObject Word.Application 打印小票
javascript 时间格式 Date.prototype.format = function (format) { var o = { "M+": this.getMonth( ...
- jdbc框架 commons-dbutils+google guice+servlet 实现一个例子
最近闲着无聊,于是看了一下jdbc框架 commons-dbutils与注入google guice. 我就简单的封装了一下代码,效率还是可以的.... jdbc+google guice+servl ...
- Eclipse项目内存溢出解决方案
方法一: 打开eclipse,选择Window--Preferences...在对话框左边的树上双击Java,再双击Installed JREs,在右边选择前面有对勾的JRE,再单击右边的“Edit” ...
- 《Linux内核设计与实现》读书笔记(七)- 中断处理【转】
转自:http://www.cnblogs.com/wang_yb/archive/2013/04/19/3030345.html 中断处理一般不是纯软件来实现的,需要硬件的支持.通过对中断的学习有助 ...
- 路由器扫描的Java源码
这个源码不是本人写的,是我原来的领导写的,我们都叫他东哥,这个是东留给我的一个小资源,好佩服他哦,这个东西可以用来扫描全世界的路由器,破解路由器账户和密码 当然是简单的了.我能力不够没有更完善的补充下 ...
- Eclipse插件 —— Maven的安装
1.下载插件 下载一(CSDN 网站下载) CSDN上提供的下载内容是笔者在SOURCEFORGE网站上下载下来的. 由于SOURCEFORGE网站上有多个版本,且没有集中打包,需逐个下 ...
- python实现简单kNN
注释写得很清楚了,熟悉了一下python的一些基本语法和numpy中的一些操作. from numpy import * import operator def createDataSet(): # ...
- [POJ2002]Squares(计算几何,二分)
题目链接:http://poj.org/problem?id=2002 给定一堆点,求这些点里哪些点可以构成正方形,题目给定n<=1000,直接枚举四个点是肯定会超时的,因此要做一些优化. 有公 ...