235. Lowest Common Ancestor of a Binary Search Tree

Posted 我的名字叫周周

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了235. Lowest Common Ancestor of a Binary Search Tree相关的知识,希望对你有一定的参考价值。

    /*
     * 235. Lowest Common Ancestor of a Binary Search Tree 
     * 2016-6-15 By Mingyang
     * 这题目更简单,下面两种方法都适用,现在给出更直接的方法
     */
    public TreeNode lowestCommonAncestor3(TreeNode root, TreeNode p, TreeNode q) {
        if (p.val == root.val || q.val == root.val)
            return root;
        if ((p.val < root.val && q.val > root.val)|| (p.val > root.val && q.val < root.val))
            return root;
        if (p.val < root.val) {
            return lowestCommonAncestor(root.left, p, q);
        } else {
            return lowestCommonAncestor(root.right, p, q);
        }
    }

 

以上是关于235. Lowest Common Ancestor of a Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章

LC.235.Lowest Common Ancestor of a Binary Search Tree

LeetCode 236. Lowest Common Ancestor of a Binary Tree; 235. Lowest Common Ancestor of a Binary Searc

#Leetcode# 235. Lowest Common Ancestor of a Binary Search Tree

235. Lowest Common Ancestor of a Binary Search Tree

235. Lowest Common Ancestor of a Binary Search Tree

235. Lowest Common Ancestor of a Binary Search Tree