题目描述:

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.

  利用二叉排序树的性质,可以很好地解决这道题。

solution:

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (root == NULL || p == NULL || q == NULL)return NULL;if (root->val > p->val && root->val > q->val)return lowestCommonAncestor(root->left, p, q);else if (root->val < p->val && root->val < q->val)return lowestCommonAncestor(root->right, p, q);else return root;
}

  如果只是一颗普通的二叉树呢?

solution:

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (!root)return NULL;if (root == p || root == q)return root;TreeNode *L = lowestCommonAncestor(root->left, p, q);TreeNode *R = lowestCommonAncestor(root->right, p, q);if (L && R)return root;return L ? L : R;
}

来源:Lowest Common Ancestor of a Binary Tree Part I

如果二叉树的结点存在指向父结点的指针,问题可以转化为求两个单链表的相交结点。

solution:

struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode *parent;
};int getHeight(TreeNode *p)
{int height = 0;while (p){height++;p = p->parent;}return height;
}TreeNode* lowestCommonAncestor(TreeNode *p, TreeNode *q)
{int h1 = getHeight(p);int h2 = getHeight(q);if (h1 > h2){swap(h1, h2);swap(p, q);}int dh = h2 - h1;for (int h = 0; h < dh; ++h)q = q->parent;while (p && q){if (p == q)return p;p = p->parent;q = q->parent;}return NULL;
}

来源:Lowest Common Ancestor of a Binary Tree Part II

转载于:https://www.cnblogs.com/gattaca/p/4674664.html

Lowest Common Ancestor of a Binary Search Tree(树中两个结点的最低公共祖先)相关推荐

  1. leetcode 235. Lowest Common Ancestor of a Binary Search Tree | 235. 二叉搜索树的最近公共祖先(哈希表)

    题目 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 题解 哈希表解法思路来自左程云< ...

  2. 235 Lowest Common Ancestor of a Binary Search Tree

    题目 235 Lowest Common Ancestor of a Binary Search Tree 因为是binary search tree,因此利用没个节点的值进行二分查找即可复杂度O(h ...

  3. Lowest Common Ancestor of a Binary Search Tree a Binary Tree

    235. Lowest Common Ancestor of a Binary Search Tree 题目链接:https://leetcode.com/problems/lowest-common ...

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

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

  6. 235. Lowest Common Ancestor of a Binary Search Tree(Tree-Easy)

    转载请注明作者和出处: http://blog.csdn.net/c406495762 Given a binary search tree (BST), find the lowest common ...

  7. Leet Code OJ 235. Lowest Common Ancestor of a Binary Search Tree [Difficulty: Easy]

    题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...

  8. 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 ...

  9. LeetCode:235. 二叉搜索树的最近公共祖先(Lowest Common Ancestor of a Binary Search Tree)

    二叉搜索树性质: 1.任意节点node,其左子树中的val不大于node.val,其右子树中的val不小于node.val. 2.不同的二叉搜索树可以代表同一组值的集合 3.二叉搜索树的基本操作和树的 ...

最新文章

  1. R语言嵌套方差分析(Nested ANOVA)实战
  2. 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 远程调用 目标进程中 libc.so 动态库中的 mmap 函数 二 | 准备参数 | 远程调用 mmap 函数 )
  3. ExtJS入门(08)窗口,按钮,输入框,
  4. 山东2021高考成绩查询时间二本,山东二本录取结果什么时候出来,2021年高考山东二本录取结果查询时间...
  5. servlet api.jar是干什么的?
  6. 为什么MySQL不建议使用NULL作为列默认值?
  7. 零基础学python还是c语言-学习汇编还是学习python,自己刚学完C语言,学习那个好呢?...
  8. Cloud Solution Architect Certification 认证备考指南
  9. # 冰冻三尺非一日之寒。
  10. MySQL窗口函数OVER()
  11. 使用keytool转换签名证书格式,keyStore、jks签名证书相互转换
  12. 深入理解Java内存模型(五)——锁
  13. d3 企业图谱 仿天眼查 企查查
  14. Maya cmds pymel 单位和轴向设置
  15. 无业务不伸缩之一,云计算有ESS(基础篇)
  16. 【python--爬虫】千图网高清背景图片爬虫
  17. glib实现Socket通信
  18. ▽算符在球坐标系_球坐标系中的角动量算符
  19. win10 路由表配置
  20. linux启动dns失败,Linux DNS 设置失败

热门文章

  1. 第十二周项目一-实现复数类中的运算符重载(3)
  2. java垃圾回收机制_乐字节Java|GC垃圾回收机制、package和import语句
  3. AtCoder AGC002E Candy Piles (博弈论)
  4. 阿里云rds linux平台使用wget 工具下载备份与日志文件
  5. tail -f 和 -F 的用法
  6. 苹果的浏览器safari无法识别 2016-1-1这样的日期,会返回Invalid Date
  7. 四则运算个人项目反思总结
  8. python处理xml中非法字符的一种思路
  9. Thrift Direct Memory OOM问题解决方法 内存溢出问题
  10. java 迭代器只遍历了一次的解决方案