Leetcode 235
思路1:对于一棵二叉排序树
1.如果当前节点的值小于p,q的值,那么LCA一定在root的右边;
2.如果当前节点的值大于p,q的值,那么LCA一定在root的左边;
3.如果当前节点的值在p,q的值之间,那么当前节点为LCA;
/**
* 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) {
if(root->val<p->val&&root->val<q->val)
return lowestCommonAncestor(root->right,p,q);
else if(root->val>p->val&&root->val>q->val)
return lowestCommonAncestor(root->left,p,q);
else return root;
}
};
思路2:直接搜索两个数p,q的值,记录下搜索过程的路径,左侧为-1,右侧为1.比对路径即可,路径开始不同的点即为LCA(即交叉点)
思路3:对于一棵二叉排序树,如果p,q均在当前节点左字树或者右子树,则根节点与p,q的值的差值同号,乘积大于零。
然后继续判断在左边还是在右边
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
while((root->val-p->val)*(root->val-q->val)>){
if(root->val-p->val>)
root=root->left;
else
root=root->right;
}
return root;
}
Leetcode 235的更多相关文章
- [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. 二叉搜索树的最近公共祖先 32
235. 二叉搜索树的最近公共祖先 235. Lowest Common Ancestor of a Binary Search Tree 题目描述 给定一个二叉搜索树,找到该树中两个指定节点的最近公 ...
- LeetCode 235. 二叉搜索树的最近公共祖先
235. 二叉搜索树的最近公共祖先 题目描述 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先 ...
- 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 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 236 二叉树两个节点的最近公共祖先
描述: 给定二叉树两个节点,求其最近公共祖先.最近即所有公共祖先中深度最深的. ps:自身也算自身的祖先. 235题解决: 这是二叉搜索树,有序的,左边小右边大. TreeNode* lowestCo ...
- [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 二叉搜索树的最近公共祖先
235. 二叉搜索树的最近公共祖先 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个 ...
- [程序员代码面试指南]二叉树问题-在二叉树中找到两个节点的最近公共祖先、[LeetCode]235. 二叉搜索树的最近公共祖先(BST)(非递归)
题目 题解 法一: 按照递归的思维去想: 递归终止条件 递归 返回值 1 如果p.q都不在root为根节点的子树中,返回null 2 如果p.q其中之一在root为根节点的子树中,返回该节点 3 如果 ...
- 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 ...
随机推荐
- Mac 安装Jupyter notebook
python:mac下自带Python 2.7.10 1.先升级了pip安装工具:sudo python -m pip install --upgrade --force pip 2.安装setupt ...
- matplotlib库解析
matplotlib 是python最著名的2D绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中.通过简单的绘图语 ...
- 四个dos命令检查你的电脑是否中木马
一些基本的命令往往可以在保护网络安全上起到很大的作用,下面几条命令的作用就非常突出. 命令是再CMD中输入,不是运行框中 一.检测网络连接 如果你怀疑自己的计算机上被别人安装了木马,或者是中了病毒,但 ...
- LeetCode-Count Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- kindeditor在Java项目中的应用以及图片上传配置
在官网下载Kindededitor的开发包 在项目中javaweb项目中导入kindeditor必须要使用的Jar包(用于文件上传,除非你的富文本编辑器不使用图片上传)jar包可以在官网的开发包中 ...
- AMD、CMD、CommonJs和 ES6对比
AMD(异步模块定义)是RequireJS在推广过程中对模块定义的规范化产出. define(['package/lib'], function(lib){ function foo(){ lib.l ...
- k8s滚动升级
为了服务升级过程中提供可持续的不中断的服务,Kubernetes 提供了rolling update机制,具体配置需要修改对应服务的yaml文件 参数解析: minReadySeconds: 100 ...
- Java操作文件转码
package downloadTest; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.F ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- Android 满屏显示自定义的View,并进行移动
新建一个类,继承View package com.topcrab.mygame; import android.content.Context; import android.graphics.Bit ...