LCA最小公共父节点解法:

1、二叉搜索树:

中序遍历是升序,前序遍历即按序插入建树的序列。

二叉搜索树建树最好用前序+中序,如果用前序建树,最坏情况会退化为线性表,超时。

最近公共祖先甲级: A1143,1151

利用二叉搜索树的性质寻找结点u和v的最低公共祖先(递归解法)

1)如果根结点的值大于max(u,v),说明u和v均在根结点的左子树,则进入根结点的左子结点继续递归

2)如果根结点的值小于min(u,v),说明u和v均在根结点的右子树,则进入根结点的右子结点继续递归

3)剩下的情况就是,根结点的值比其中一个值大,比另外一个小,则该结点就是结点u,v的最低公共祖先了

二叉搜索树核心代码:

Node* getLCA(Node* root,int u,int v)
{
if(root==nullptr) return nullptr;
if(root->val > max(u,v)) return getLCA(root->lchild,u,v);
else if(root->val < min(u,v)) return getLCA(root->rchild,u,v);
else return root;
}

2、普通二叉树

递归解法

在递归函数中,我们首先判断当前结点是否为空,若为空,直接返回;或者,当前结点是否就是u或v,若是,也直接返回该结点。否则,就对其左右孩子结点分别调用递归函数。可以想到,结点u和v有以下三种情况:

  1)u和v分别位于当前结点的左右两侧;

  2)u和v都在当前结点的左子树中;

  3)u和v都在当前结点的右子树中。

若u和v分别位于左右子树中,那么对左右子结点调用递归函数,会分别返回u和v结点的位置,而当前结点正好就是u和v的最小共同父结点,直接返回当前结点即可。

若u和v同时位于左子树,这里有两种情况,一种情况是left会返回u和v中较高的那个位置,而right会返回空,所以我们最终返回非空的left即可;还有一种情况是会返回u和v的最小父结点,就是说当前结点的左子树中的某个结点才是u和v的最小父结点,会被返回。

若u和v同时位于右子树,同样这里有两种情况,一种情况是right会返回u和v中较高的那个位置,而left会返回空,所以我们最终返回非空的right即可,还有一种情况是会返回u和v的最小父结点,就是说当前结点的右子树中的某个结点才是u和v的最小父结点,会被返回。

核心代码:

Node* getLCA(Node* root,int u,int v)
{
if(root==nullptr || root->val==u || root->val==v) return root;
Node* left=getLCA(root->lchild,u,v);
Node* right=getLCA(root->rchild,u,v);
if(left && right) return root;
else return (left?left:right);
}

参考:https://www.cnblogs.com/kkmjy/p/9529771.html

LCA最小公共父节点的解题思路的更多相关文章

  1. [LeetCode] 1123. Lowest Common Ancestor of Deepest Leaves 最深叶结点的最小公共父节点

    Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall that: Th ...

  2. [LeetCode] 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 ...

  3. [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 BS ...

  4. 笔试算法题(24):找出出现次数超过一半的元素 & 二叉树最近公共父节点

    出题:数组中有一个数字出现的次数超过了数组长度的一半,请找出这个数字: 分析: 解法1:首先对数组进行排序,时间复杂度为O(NlogN),由于有一个数字出现次数超过了数组的一半,所以如果二分数组的话, ...

  5. [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 ...

  6. [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 ...

  7. 二叉树中两节点的最近公共父节点(360的c++一面问题)

    面试官的问题:写一个函数  TreeNode* Find(TreeNode* root, TreeNode* p, TreeNode* q) ,返回二叉树中p和q的最近公共父节点. 本人反应:当时有点 ...

  8. [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

    4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...

  9. LeetCode 二叉树,两个子节点的最近的公共父节点

    LeetCode 二叉树,两个子节点的最近的公共父节点 二叉树 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共父亲节点 https://leetcod ...

随机推荐

  1. Python基础-使用paramiko

    一:简介 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. 由于使用的是python这样的能够跨平台运行的语言,所以所有python支 ...

  2. hdu4059 The Boss on Mars 容斥原理

    On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger bo ...

  3. LeetCode - Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  4. oracle命令导入SQL脚本

    使用@导入 比如说我在oracle家目录下有a.sql文件 命令行sqlplus / as sysdba,进入后 SQL>@/home/oracle/a.sql; 回车搞定

  5. 利用反射C#获取事件列表

    在程序设计中有时候需要动态订阅客户自己的事件,调用完成后又要删除以前订阅的事件.因为如果不删除,有时会造成事件是会重复订阅,导致程序运行异常.一个办法是用反射来控件事件列表.清空方法代码如下: /// ...

  6. Nginx可以做什么?看完这篇你就懂了

    本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可能介绍的不完整,毕竟只是我个人使用过和了解到过得,欢迎留言交流. Nginx能做什么 —— ...

  7. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法

    System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...

  8. ElasticSearch(二) 关于DSL

    关于Lucene里面的查询评分,其实是基于一个公式:TF/ IDF(Term-Frequency/ Inverse Document Frequency),词频率/ 倒排文档频率,这个公式讲了一个故事 ...

  9. Redis select选择数据库

    Redis的功能真是强大,可以做数据库,可以做缓存. 今天发现Redis支持分隔操作空间,使得空间与空间之间互不影响. SELECT index 切换到指定的数据库,数据库索引号 index 用数字值 ...

  10. RedHat6.5安装Spark单机

    版本号: RedHat6.5   RHEL 6.5系统安装配置图解教程(rhel-server-6.5) JDK1.8      http://blog.csdn.net/chongxin1/arti ...