72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)
题目描述:
一个二叉搜索树,给定两个节点a,b,求最小的公共祖先
_______6______
/ \
___2__ ___8__
/ \ / \
0 _4 7 9
/ \
3 5
例如:
2,8 —->6 2,4—–>2
原文描述:
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.
思路分析:
- 二叉树考虑递归的思路
- 如果a < = root <= b,可以确定root是lct
- 采用二分搜索的方式递归,root > a,b,继续遍历左子树,反之右边的子树
-在递归的开始判断空值的情况,直接返回root
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || p == null || q == null)
return root;
if(p.val > root.val && q.val > root.val)
return lowestCommonAncestor(root.right, p, q);
else if(p.val < root.val && q.val < root.val)
return lowestCommonAncestor(root.left, p, q);
else
return root;
}
}
更多的leetcode的经典算法,查看我的leetcode专栏,链接如下:
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)的更多相关文章
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)
Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...
- 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...
- 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 ...
- 【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 ...
- LeetCode OJ 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 ...
- 【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 th ...
- LeetCode OJ: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 ...
随机推荐
- oracle拆分一个连续的字符串
create or replace procedure pc( sss out varchar2)isstr varchar2(20):='ph,p,cod,do,cu';en integer:=i ...
- 再谈RunLoop
RunLoop 一 概述: 一句话解释RunLoop:运行任务的循环. 为什么要有RunLoop:解决交互式UI设计中的一个问题,如何快速响应用户输入,如何快速将程序运行结果输出到屏幕? 计算机是个笨 ...
- Swift 3.0项目迁移的一些记录
刚执行完Convert后报错600+,真是令人奔溃. 之后重新编译,仔细分析后发现其实真实错误远没有那么多.最终实际修改到的错误也就几十个,而且其中某些还是同一种错误. 这个项目是一个供自己使用的浏览 ...
- Appium--swipe滑动方法
最近公司要求对APP模块自动化,以Android 自动化为例,把appium滑动的方法swipe()再小结下.滑动的目的,一方面是为了更好的查找元素,一方面就是为了滑屏操作.代码如下: package ...
- Oracle性能优化-读懂执行计划
Oracle的执行计划 得到执行计划的方式 Autotrace例子 使用Explain explain plan set STATEMENT_ID='testplan' for select * fr ...
- 深入Java虚拟机(4)——网络移动性
一.软件应用程序发展的几个阶段 软件应用程序发展经历了如下几个阶段: 服务于多个终端用户的大型计算机系统 孤立的个人计算机上运行孤立的软件 客户机/服务器模式 分布式处理模式 内容服务模式(网络移动性 ...
- Xcode中lldb的REPL调试方法
Xcode中lldb调试器有一个repl语句,可以用来模拟swift解释器的REPL行为,即Read Eval Print Loop. 在Xcode里随意打开程序,中断入调试器.在调试控制台中输入re ...
- C/C++的mem函数和strcpy函数的区别和应用
mem系列函数是面试的时候常考的知识点,我们需要熟练掌握这三个函数的原理和代码实现,要能准确无误的写出代码. memcpy.memset和memset三个函数在使用过程中,均需包含以下头文件: //在 ...
- FFmpeg源代码简单分析:av_find_decoder()和av_find_encoder()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- Citrix 桌面虚拟化解决方案与VMware桌面虚拟化解决方案对比
通过 XenDesktop 和 FlexCast为各种场景交付虚拟桌面 企业桌面面临的问题 为每个用户提供安全高效的桌面环境是几乎所有公司或组织的基本要求.如果用户无法使用他们的桌面或应用程序,公司就 ...