leetcode: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__
/ \ / \
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.
分析:由于在所谓的二叉搜索树(binary search tree)中,处处满足顺序性(即任一节点的左(右)子树种,所有节点均小于(大于)r)。
思路: 1、当头结点为空时,返回空指针
2、如果节点p、q的值都比root的值要小,那么LCA一定为root的左子树;而如果节点p、q的值都比root的值要大,那么LCA一定为root的右子树;当节点p、q的值中一个比root的值要大,另一个比它小时,LCA就是root了。
3、我们要考虑是否能覆盖到节点是它自身子节点的情况,这时返回的是p或q的其中一个。
代码如下:(recursive solution)
/**
* 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) {
while(root !=nullptr){
if(p->val < root->val && q->val < root->val){
return lowestCommonAncestor(root->left,p,q);
}
else if(p->val > root->val && q->val > root->val){
return lowestCommonAncestor(root->right,p,q);
}
else return root; // 当p->val = root->val或 q->val =root->val时,就是节点是它自身子节点的情况了
}
return root;
}
};
也可以简洁点:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (p -> val < root -> val && q -> val < root -> val)
return lowestCommonAncestor(root -> left, p, q);
if (p -> val > root -> val && q -> val > root -> val)
return lowestCommonAncestor(root -> right, p, q);
return root;
}
};
其他参考解法:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(p->val > root->val && q->val < root->val)
{
return root;
}
if(p->val < root->val && q->val > root->val)
{
return root;
}
if( p->val == root->val || q->val == root->val)
return root; if( p->val < root->val && q->val < root->val)
return lowestCommonAncestor(root->left, p, q);
if( p->val > root->val && q->val > root->val)
return lowestCommonAncestor(root->right, p, q);
}
};
或:(iterative solution)
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode* cur = root;
while (true) {
if (p -> val < cur -> val && q -> val < cur -> val)
cur = cur -> left;
else if (p -> val > cur -> val && q -> val > cur -> val)
cur = cur -> right;
else return cur;
}
}
};
leetcode:Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- 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 二叉搜索树的最小共同父节点
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 二叉搜索树的最近公共祖先
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 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 (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- (easy)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 BS ...
- Java [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 ...
- LeetCode (236):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
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- linux下解压命令大全(转载)
.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...
- SWF Web播放器
<HTML> <HEAD> <!-- saved from url=(0013)about:internet --> <TITLE> Untitled. ...
- Sqli-labs less 54
第四部分/page-4 Challenges Less-54 此系列主要是一个进阶的学习,将前面学到的知识进行更深次的运用.这一关我们主要考察的依旧是字符型注入,但是只能尝试十次.所以需要在尝试的时候 ...
- Redis与Memcached的incr/decr差异对比
目前广泛使用的分布式缓存Redis和Memcached均支持对整数型Value值的增减,对应到具体命令中就是incr和decr命令. incr/decr是原子性操作(memcached 1.2.4及以 ...
- HDU3341 Lost's revenge(AC自动机&&dp)
一看到ACGT就会想起AC自动机上的dp,这种奇怪的联想可能是源于某道叫DNA什么的题的. 题意,给你很多个长度不大于10的小串,小串最多有50个,然后有一个长度<40的串,然后让你将这个这个长 ...
- (转)Learning to Rank for IR的评价指标—MAP,NDCG,MRR
转自:http://www.cnblogs.com/eyeszjwang/articles/2368087.html MAP(Mean Average Precision):单个主题的平均准确率是每篇 ...
- 后缀树系列一:概念以及实现原理( the Ukkonen algorithm)
首先说明一下后缀树系列一共会有三篇文章,本文先介绍基本概念以及如何线性时间内构件后缀树,第二篇文章会详细介绍怎么实现后缀树(包含实现代码),第三篇会着重谈一谈后缀树的应用. 本文分为三个部分, 首先介 ...
- LA 3713
The Bandulu Space Agency (BSA) has plans for the following three space missions: Mission A: Landing ...
- spring_150804_controller
实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...
- Template
创建win32应用程序空工程 //main.cpp//time: 01/08/2013 #include<d3d9.h>#include <d3dx9.h> #pragma c ...