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;
    }
}


sortList and Lowest Common Ancestor of a Binary Search Tree

首先解析二叉树 Lowest Common Ancestor of a Binary Search Tree ,寻找二叉树中某两个节点中路径最短的根节点。如下,2与8 ,最短路径根节点为6. __...

[LeetCode]235. Lowest Common Ancestor of a Binary Search Tree

Problem DescriptionGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two gi...

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

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

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

LeetCode 235. 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 low...

[Java]寻找最低公共祖先(排序二叉树)Lowest Common Ancestor of a Binary Search Tree

leetcode 原题链接 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ Give...
  • lyy_hit
  • lyy_hit
  • 2015年08月15日 21:55
  • 818

235. Lowest Common Ancestor of a Binary Search Tree

leetcode 235 - Easy

LeetCode 235 Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最小公共祖先)

翻译给定一个二叉搜索树(BST),找出两个给定节点的最小公共祖先(LCA)。根据维基百科对于LCA的定义:“最小公共祖先的定义是对于两个节点v和s有一个最小的节点T, 以至于v和s都是T的后代(其中我...
  • NoMasp
  • NoMasp
  • 2016年01月12日 19:21
  • 1943

【LeetCode】Lowest Common Ancestor of a Binary Search Tree 解题报告

Lowest Common Ancestor of a Binary Search Tree[LeetCode]https://leetcode.com/problems/lowest-common-...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Lowest Common Ancestor of a Binary Search Tree
举报原因:
原因补充:

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