Lowest Common Ancestor of a Binary Search Tree

原创 2015年11月19日 12:10:18

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.

/**
 * 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) {
        int largerVal = p.val > q.val? p.val : q.val;
        int smallerVal = p.val == largerVal? q.val:p.val;
        return lowestCommonAncestorHelper(root, largerVal, smallerVal);
    }
    
    private TreeNode lowestCommonAncestorHelper(TreeNode root, int largerInput, int smallerInput){
        if(smallerInput > root.val)
            return lowestCommonAncestorHelper(root.right, largerInput, smallerInput);
        else if(largerInput < root.val)
            return lowestCommonAncestorHelper(root.left, largerInput, smallerInput);
        else
            return root;
    }
}


235. Lowest Common Ancestor of a Binary Search Tree [easy] (Python)

题目链接https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/题目原文 Given a bina...
  • coder_orz
  • coder_orz
  • 2016年05月25日 15:26
  • 888

leetcode 235: Lowest Common Ancestor of a Binary Search Tree

leetcode 235: Lowest Common Ancestor of a Binary Search Tree python, java, c++
  • xudli
  • xudli
  • 2015年07月11日 06:34
  • 10634

[Java]LeetCode235 Lowest Common Ancestor of a Binary Search Tree

[Java]LeetCode235 Lowest Common Ancestor of a Binary Search Tree
  • fumier
  • fumier
  • 2015年08月22日 21:31
  • 735

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...
  • sunao2002002
  • sunao2002002
  • 2015年07月27日 18:41
  • 2164

[LeetCode 236] Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accord...
  • sbitswc
  • sbitswc
  • 2015年08月25日 13:31
  • 2438

leetcode 236: Lowest Common Ancestor of a Binary Tree

leetcode 236: Lowest Common Ancestor of a Binary Tree PYTHON, JAVA, C++
  • xudli
  • xudli
  • 2015年07月14日 02:35
  • 6343

[Java]Leetcode236 Lowest Common Ancestor of a Binary Tree

求二叉树的公共父结点
  • fumier
  • fumier
  • 2015年08月23日 08:58
  • 853

LeetCode 236. Lowest Common Ancestor of a Binary Tree(二叉树的最低公共祖先)

原题网址:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, f...
  • jmspan
  • jmspan
  • 2016年04月06日 06:43
  • 588

leetcode 236. Lowest Common Ancestor of a Binary Tree-二叉树共同祖先|深度遍历|递归|非递归

原题链接:236. Lowest Common Ancestor of a Binary Tree 【思路-Java】dfs|递归实现 本题是二叉树的深度遍历的典型应用,基础还是二叉树的遍历。 ...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016年06月11日 11:44
  • 688

P2P点播系统性能优化(3): Tracker服务器选择最优节点

在集中式的P2P网络系统中,Tracker服务器的主要功能是提供最优节点检索功能 ;Tracker服务器的最优节点选择算法如下:                                1....
  • checo_chua
  • checo_chua
  • 2011年08月17日 14:52
  • 666
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Lowest Common Ancestor of a Binary Search Tree
举报原因:
原因补充:

(最多只允许输入30个字)