235 Lowest Common Ancestor of a Binary Search Tree
2015-07-21 20:08
344 查看
/** * 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 null; if(Math.max(p.val, q.val) < root.val) { return lowestCommonAncestor(root.left, p, q); } else if(Math.min(p.val, q.val) > root.val) { return lowestCommonAncestor(root.right, p, q); } else return root; } }
相关文章推荐
- 机器学习中的数学(2)-线性回归,偏差、方差权衡
- HDU 5289 Assignment (数字序列,ST算法)
- 二分图最大匹配问题及其扩展
- 阿里巴巴店铺排名优化程序共享
- 统计字符
- 机器学习中的数学(1)-回归(regression)、梯度下降(gradient descent)
- Java系列学习(十四)-集合
- lx-0721-1001字符串统计
- iOS开发-UITableViewCell(UICollectionViewItem)需要两个手指点击或者长按才可以选中,解决办法
- Semantic segmentation (with deconvolutional CNN) 作者公布源码和模型
- linux的子进程调用exec( )系列函数
- 多校第一场 1001 hdu 5288 OO’s Sequence(枚举)
- 编译的依赖不能vs的release工程
- 左连接,右连接,内连接
- web开发必备工具
- Gradle版本冲突
- JAVA基础之内部类
- hadoop-2.6.0集群实例(接上一篇)
- 自定义Application的未捕获异常处理
- 【bzoj2084】 POI2010 Antisymmetry manacher