递归实现如下:

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
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);
return root;
}

Java for LeetCode 235 Lowest Common Ancestor of a Binary Search Tree的更多相关文章

  1. 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 利用二 ...

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

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

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

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

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

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

  8. Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树

    给定一个二叉搜索树的两个节点,找出他们的最近公共祖先,如, _______6______ / \ ___2__ ___8__ / \ / \ 0 4 7 9 / \ 3 5 2和8的最近公共祖先是6, ...

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

随机推荐

  1. 说说移动前端中 viewport (视口)

    转载网络资源的文章:来源不详~~ 移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dip ( device-independent pixel 设 ...

  2. CSS 使用推荐

      中文字体css编码转换 微软雅黑 \5FAE\8F6F\96C5\9ED1 或 Microsoft YaHei 黑体 \9ED1\4F53 新宋体 \65b0\5b8b\4f53 宋体 \5b8b ...

  3. 【转载】VC维的来龙去脉

    本文转载自 火光摇曳 原文链接:VC维的来龙去脉 目录: 说说历史 Hoeffding不等式 Connection to Learning 学习可行的两个核心条件 Effective Number o ...

  4. PHP协程 详解

    [开源中国] PHP 使用协同程序实现合作多任务 [风雪之隅] 在PHP中使用协程实现多任务调度

  5. Swift---TextView用法

    1.TextView放在一个NSScrollView中,作为documentView存在. @IBOutlet weak var txtScrollView: NSScrollView! 2.在Tex ...

  6. Cache and Virtual Memory

    Cache存储器:电脑中为高速缓冲存储器,是位于CPU和主存储器DRAM(DynamicRandonAccessMemory)之间,规模较小,但速度很高的存储器,通常由SRAM(StaticRando ...

  7. objective-c-sort-keys-of-nsdictionary-based-on-dictionary-entries

    NSArray *keys = [someDictionary allKeys]; NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NS ...

  8. 关于css3的动画总结

    旋转:transform:rotate(xxdeg)扭曲:transform:skey(x,y)缩放:transform:scale(x,y)变形位移:transform:translate(x,y) ...

  9. Oracle 中循环遍历某张表,并对符合条件的进行Update操作

    BEGIN FOR L_RECORD IN (select RECORD_ID,CURR_PERIOD,PERIOD_START_DATE, (sysdate- PERIOD_START_DATE) ...

  10. 转一篇简易易懂的android回调的实现--->(转的)

    回调机制在 Android 监听用户界面操作中的体现   本文讨论以下两个内容: 1. 回调函数 2. 回调机制在 Android框架 监听用户界面操作中的作用 一 回调函数 回调函数就是一个通过函数 ...