LeetCode (236):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 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与节点b的公共祖先c一定满足:a与b分别出现在c的左右子树上(如果a或者b本身不是祖先的话)。
Java代码:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null) 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!=null&&R!=null) return root;
return L!=null?L:R;
}
//test
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode tr=new TreeNode(1);
TreeNode nodeB=new TreeNode(2);
TreeNode nodeC=new TreeNode(3);
TreeNode nodeD=new TreeNode(4);
TreeNode nodeE=new TreeNode(5);
tr.left=nodeB;
tr.right=nodeC;
tr.left.right=nodeD;
tr.left.left=nodeE;
System.out.print(lowestCommonAncestor(tr, nodeC, nodeE).val);
}
}
LeetCode (236):Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- [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 ...
- 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先
235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowes ...
- [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 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 ...
- (easy)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: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 ...
- Java [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 ...
随机推荐
- 160513、nginx+tomcat集群+session共享(linux)
第一步:linux中多个tomcat安装和jdk安装(略) 第二步:nginx安装,linux中安装nginx和windows上有点不同也容易出错,需要编译,这里做介绍 一.安装依赖 gcc open ...
- c# 解析json 字符串 报异常 Bad JSON escape sequence 解决方案
当我试图将一个完整的本地路径的字符串串(如:c:\\aaa\\数学题\\三一班\\ea15ae66-d5cd-4244-87e4-fcf97b06b407.jpg)encodeURL之后当做一个页面参 ...
- Struts2+Spring3+MyBatis3整合以及Spring注解开发
分类: Web(2) 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在做一个SpringMVC+spring+MyBatis的项目,突然想起以前自己要搭建一个Struts2+Sprin ...
- 转发URL请求
如何用Netty实现一个轻量级的HTTP代理服务器 - Newland - 博客园 https://www.cnblogs.com/jietang/p/8926325.html 现在迫切希望有一个HT ...
- timeline css
CODE <!doctype html> <html> <head> <meta charset="UTF-8"> <titl ...
- qt and redis desktop manager
(ubuntu desktop)http://blog.csdn.net/ficksong/article/details/7497827 redis manager in ubuntu wget h ...
- MFC工具栏设计
工具栏中包含了一组用于执行命令的按钮,每个按钮都用一个图标来表示.当单击某个按钮时,会产生一个相应的消息,对这个消息的处理就是按钮的功能实现.将菜单中常用的功能放置在工具栏中,这样可以方便用户操作,省 ...
- Celery(一个懂得 异步任务、定时任务、周期任务 的"芹菜")
一.什么是Celery? Celery 是基于Python实现的模块,用于执行异步.定时.周期任务的,其结构的组成是: - 用户任务 app - 管道 broker 用于存储任务(官方推荐 redis ...
- 小技巧-如何加快github下载代码的速度(转)
作为开发人员,github是大家的标配了,常常会苦恼于gitclone某个项目的时候速度太慢,看着控制台那几K十几K的龟速,吐血!! 原因很简单:github的CDN被伟大的墙屏蔽所致. 所以解决方案 ...
- JVM堆内存相关的启动参数:年轻代、老年代和永久代的内存分配
如果想观察JVM进程占用的堆内存,可以通过命令工具jmap或者可视化工具jvisualvm.exe.JVM这些启动参数都拥有默认值,如果想了解JVM的内存分配策略,最好手动设置这些启动参数.再通过JD ...