LCA最小公共父节点解法:

1、二叉搜索树:

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

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

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

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

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

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

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

二叉搜索树核心代码:

  1. Node* getLCA(Node* root,int u,int v)
  2. {
  3. if(root==nullptr) return nullptr;
  4. if(root->val > max(u,v)) return getLCA(root->lchild,u,v);
  5. else if(root->val < min(u,v)) return getLCA(root->rchild,u,v);
  6. else return root;
  7. }

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的最小父结点,会被返回。

核心代码:

  1. Node* getLCA(Node* root,int u,int v)
  2. {
  3. if(root==nullptr || root->val==u || root->val==v) return root;
  4. Node* left=getLCA(root->lchild,u,v);
  5. Node* right=getLCA(root->rchild,u,v);
  6. if(left && right) return root;
  7. else return (left?left:right);
  8. }

参考: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. Comet OJ - Contest #2 简要题解

    Comet OJ - Contest #2 简要题解 cometoj A 模拟,复杂度是对数级的. code B 易知\(p\in[l,r]\),且最终的利润关于\(p\)的表达式为\(\frac{( ...

  2. JAVA静态代码块的作用

    一 般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情 况下,其他程序来调用的时候,需要使用静态方法,这种 ...

  3. 系统编程概念(文件系统mount等函数的使用)

    挂载文件系统 #include<sys/mount.h> int mount(const char *source,const char *target,const char *fstyp ...

  4. 03基于python玩转人工智能最火框架之TensorFlow介绍

    一句话介绍: Google开源的基于数据流图的科学计算库,适用于机器学习 不局限于机器学习,但目前被大多用于机器学习等. TensorFlow计算流图的概念图 Tensor在图中流动. TensorF ...

  5. layout 布局、手风琴accordion、选项卡tabs【转载】

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. maven 阿里云 国内镜像 中央仓库

    修改maven根目录下的conf文件夹中的setting.xml文件,具体内容和示意图如下: <mirror> <id>alimaven</id> <name ...

  7. opengl 几何着色器

    绘制4条线段 #define GLEW_STATIC #include <GL/glew.h> #include <GLFW/glfw3.h> #include "S ...

  8. LOJ 3055 「HNOI2019」JOJO—— kmp自动机+主席树

    题目:https://loj.ac/problem/3055 先写了暴力.本来想的是 n<=300 的那个在树上暴力维护好整个字符串, x=1 的那个用主席树维护好字符串和 nxt 数组.但 x ...

  9. JS replace()用法实现replaceAll

    由于js没有提供replaceAll,则使用正则表达式实现全局替换. javascript中replace() 方法如果直接用str.replace("-","!&quo ...

  10. Git-git rebase详解

    git合并代码方式主要有两种方式,分别为:1.merge处理,这是大家比较能理解的方式.2.rebase处理,中文此处翻译为衍合过程. git rebase操作讲解例子: cd /usr/local/ ...