转载请注明作者和出处: http://blog.csdn.net/c406495762

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).”

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.

题目:求二叉树的LCA,也就是两个节点的最低公共祖先。比如上图的二叉树,节点2和8的LCA是6,节点2和4的LCA是2。

思路:观察给定的二叉树可知,二叉树中任何节点:左节点的值 < 根节点的值 < 右节点的值。根据这个性质,可以做出如下判断:

  • 如果p、q都比根节点小,则在左子树中递归查找最低公共祖先节点。
  • 如果p、q都比根节点大,则在右子树中递归查找最低公共祖先节点。
  • 如果p、q一个比根节点大,一个比根节点小,或者有一个等于根节点,则根节点即为最低公共祖先节点。

Language : cpp

递归方法:

/*** 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 (p->val < root->val && root->val > q->val)return lowestCommonAncestor(root->left, p, q);else if (p->val > root->val && root->val < q->val)return lowestCommonAncestor(root->right, p, q);elsereturn root;}
};

Language : python

递归方法:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution(object):def lowestCommonAncestor(self, root, p, q):""":type root: TreeNode:type p: TreeNode:type q: TreeNode:rtype: TreeNode"""if p.val < root.val > q.val:return self.lowestCommonAncestor(root.left, p, q)elif p.val > root.val < q.val:return self.lowestCommonAncestor(root.right, p, q)else:return root

迭代方法:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution(object):def lowestCommonAncestor(self, root, p, q):""":type root: TreeNode:type p: TreeNode:type q: TreeNode:rtype: TreeNode"""while True:if p.val < root.val > q.val:root = root.leftelif p.val > root.val < q.val:root = root.rightelse:return root

代码获取: https://github.com/Jack-Cherish/LeetCode

235. Lowest Common Ancestor of a Binary Search Tree(Tree-Easy)相关推荐

  1. 235 Lowest Common Ancestor of a Binary Search Tree

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

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

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

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

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

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

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

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

  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. 什么是闭包?变量作用域和闭包。
  2. JavaScript 精粹 基础 进阶(4)对象
  3. .net core快速上手
  4. c++中的void指针和const指针
  5. python推荐哪个系统好_什么是好的推荐系统?
  6. boost::geometry::closeable_view用法的测试程序
  7. ES6标准入门(第二版)pdf
  8. 使用layer.tips实现鼠标悬浮时触发事件提示消息实现
  9. 一天完成一点,进度太慢了啊
  10. 查看其他计算机的共享资源,NetResView (共享资源查看)
  11. 源码解析——消息机制
  12. python中质数的表达方式_python求质数的3种方法
  13. 《深入浅出MFC》第三章 MFC六大关键技术之仿真
  14. Python 遗传算法路径规划
  15. vscode如何调整字体大小
  16. Python之面向对象-类与 类之间的关系
  17. 人工智能的示例——八皇后问题
  18. flask python 上传图片或头像
  19. Oracle数据库常用语句使用记录
  20. windows11+wsl+clion调试srs

热门文章

  1. 关于vue组件的销毁与重载
  2. Midjourney生成LOGO指南
  3. 链路聚合(三层链路聚合)
  4. 自顶向下 谢希仁计算机网络第七版课后答案
  5. 将SD卡伪装成系统硬盘
  6. iPhone手机怎么把提醒事项显示在桌面上
  7. Linux Ansible自动化运维 copy模块
  8. php sql 18456,1、mssql2019报18456不能登录
  9. android观察者模式被观察者,Android的设计模式-观察者模式
  10. html vh和百分比,css中height 100vh的应用场景,动态高度百分比布局,浏览器视区大小单位...