题目 235 Lowest Common Ancestor of a Binary Search Tree

因为是binary search tree,因此利用没个节点的值进行二分查找即可复杂度O(h)

class Solution:# @param {TreeNode} root# @param {TreeNode} p# @param {TreeNode} q# @return {TreeNode}def lowestCommonAncestor(self, root, p, q):if p.val > q.val:p,q = q,pif root.val in [p.val, q.val]:return rootif p.val < root.val < q.val:return rootelif p.val < root.val and q.val < root.val:return self.lowestCommonAncestor(root.left, p, q)else:return self.lowestCommonAncestor(root.right, p, q)

转载于:https://www.cnblogs.com/dapanshe/p/4641982.html

235 Lowest Common Ancestor of a Binary Search Tree相关推荐

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

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

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

  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. JavaScript 之 typeof
  2. Django DTL模板语法中的循环
  3. EXCEL数据导入dataset
  4. Day1 - Python基础1
  5. glibc库详解及与POSIX,system V这些库之间关系的说明
  6. spring aop实现日志收集
  7. html cellpadding属性,HTML table标签 cellpadding 属性
  8. vue项目中使用特殊字体
  9. 利用matlab使用窗函数,MATLAB中的窗函数
  10. 管理信息系统期末测试题
  11. 卸载Microsoft Edge浏览器
  12. 混合波束成形|进阶:深入浅出混合波束赋形
  13. Azure机器学习模型搭建
  14. (Golang)外观模式 VS 工厂模式
  15. jasig cas java示例_单点登录cas jasig学习笔记
  16. Ubuntu下使用ipmsg(飞鸽传书)
  17. 2023口腔护理行业分析:市场需求多元化,细分市场持续多变
  18. 梅科尔工作室——Django+HarmonyOS实现用户登录
  19. 世界上最好的语言是哪个?
  20. 志宇-RabbitMQ学习

热门文章

  1. python做电脑软件-程序员带你十天快速入门Python,玩转电脑软件开发(二)
  2. python3最新版本-mac下安装Python3.*(最新版本)
  3. python怎么用excel-Python使用xlwt模块操作Excel的方法详解
  4. python的工资为什么这么低-为什么学完Python找不到工作?原因如下!
  5. python编程实例视屏-使用Python实现视频下载功能实例代码
  6. python贴吧爬虫-Python爬虫——抓取贴吧帖子
  7. python一点基础都没有的怎么办-你们都是怎么学 Python 的?
  8. python语言入门pdf-Python编程从入门到精通.pdf
  9. 学python的好处-python有什么优势
  10. python数据处理实例-Python数据预处理实例详解