题目链接:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/

方法一 迭代

1 方法思想

2 代码实现

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/class Solution {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {while (true) {if (root.val > p.val && root.val > q.val) {root = root.left;} else if (root.val < p.val && root.val < q.val) {root = root.right;} else {break;}}return root;}
}

3 复杂度分析

时间复杂度:
空间复杂度:

4 涉及到知识点

5 总结

方法二 递归

1 方法思想

2 代码实现

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/class Solution {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);return root;}
}

3 复杂度分析

时间复杂度:
空间复杂度:

4 涉及到知识点

5 总结

Leetcode 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. leetcode 235. Lowest Common Ancestor of a Binary Search Tree | 235. 二叉搜索树的最近公共祖先(哈希表)

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

  3. 235 Lowest Common Ancestor of a Binary Search Tree

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

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

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

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

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

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

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

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

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

最新文章

  1. 下载r包IlluminaHumanMethylation450kanno.ilmn12.hg19
  2. 《高新技术企业知识产权管理》阅读笔记
  3. JavaParser中AST节点的观察者
  4. python 程序打包 vscode_使用VScode编写python程序并打包成.exe文件
  5. c语言argc,C语言 argc和argv
  6. 浏览器插件之ActiveX开发(三)
  7. Javascript实现子窗口向父窗口传值(转)
  8. C#中的线程(三)多线程
  9. POI--各种样式的XSSFCellStyle的生成
  10. 用java开发pc软件
  11. 基于SpringBoot的社团管理系统的设计与实现
  12. uva 11137 Ingenuous Cubrency
  13. 计算机类课题研究方法,课题研究方法有哪些
  14. 学计算机专业的人,哪些人适合学习计算机专业?
  15. 【自学Python:Day3】放假的心该怎么冷静下来学……
  16. STM32 之 HAL库
  17. SpringCloud微服务网关技术——Gateway网关的使用
  18. python读取csv文件表头_python读csv文件时指定行为表头或无表头的方法
  19. Chart.xkcd图表库
  20. MapReduce 的基本原理

热门文章

  1. myeclipse使用(技术和快捷键)
  2. 计算机无法显示移动硬盘,谁知道移动硬盘在电脑显示不出来是怎么回事?
  3. 哥尼斯堡的“七桥问题”
  4. 什么是智能DNS云解析?云解析如何实现智能解析效果?
  5. mysql无参的存储过程_创建无参的存储过程(四十七)
  6. 【STM32】关于DMA控制器的介绍和使用
  7. 日记侠:你的文章为什么阅读量会那么高?
  8. 11. Zigbee应用程序框架开发指南 - 命令行接口(CLI)
  9. 计算机文件夹加密码怎么设置,如何为文件夹设置密码,教您如何设置
  10. 当面试官问你期望的薪资是多少的时候,他是这样回答的...