235. Lowest Common Ancestor of a Binary Search Tree

Easy

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 p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Given binary search tree:  root = [6,2,8,0,4,7,9,null,null,3,5]

Example 1:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.

Example 2:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

Note:

  • All of the nodes' values will be unique.
  • p and q are different and both values will exist in the BST.
package leetcode.easy;

/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class LowestCommonAncestorOfABinarySearchTree {
public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) { // Value of current node or parent node.
int parentVal = root.val; // Value of p
int pVal = p.val; // Value of q;
int qVal = q.val; if (pVal > parentVal && qVal > parentVal) {
// If both p and q are greater than parent
return lowestCommonAncestor1(root.right, p, q);
} else if (pVal < parentVal && qVal < parentVal) {
// If both p and q are lesser than parent
return lowestCommonAncestor1(root.left, p, q);
} else {
// We have found the split point, i.e. the LCA node.
return root;
}
} public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) { // Value of p
int pVal = p.val; // Value of q;
int qVal = q.val; // Start from the root node of the tree
TreeNode node = root; // Traverse the tree
while (node != null) { // Value of ancestor/parent node.
int parentVal = node.val; if (pVal > parentVal && qVal > parentVal) {
// If both p and q are greater than parent
node = node.right;
} else if (pVal < parentVal && qVal < parentVal) {
// If both p and q are lesser than parent
node = node.left;
} else {
// We have found the split point, i.e. the LCA node.
return node;
}
}
return null;
} @org.junit.Test
public void test() {
TreeNode tn11 = new TreeNode(6);
TreeNode tn21 = new TreeNode(2);
TreeNode tn22 = new TreeNode(8);
TreeNode tn31 = new TreeNode(0);
TreeNode tn32 = new TreeNode(4);
TreeNode tn33 = new TreeNode(7);
TreeNode tn34 = new TreeNode(9);
TreeNode tn43 = new TreeNode(3);
TreeNode tn44 = new TreeNode(5);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = tn31;
tn21.right = tn32;
tn22.left = tn33;
tn22.right = tn34;
tn31.left = null;
tn31.right = null;
tn32.left = tn43;
tn32.right = tn44;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = null;
tn43.left = null;
tn44.right = null;
System.out.println(lowestCommonAncestor1(tn11, tn21, tn22).val);
System.out.println(lowestCommonAncestor1(tn11, tn21, tn32).val);
System.out.println(lowestCommonAncestor2(tn11, tn21, tn22).val);
System.out.println(lowestCommonAncestor2(tn11, tn21, tn32).val);
}
}

LeetCode_235. Lowest Common Ancestor of a Binary Search Tree的更多相关文章

  1. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

  2. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  3. Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree

    1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...

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

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

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

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

  8. [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

    4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...

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

随机推荐

  1. spark如何划分DAG视图

    spark根据宽依赖进行DAG视图的划分. 1.窄依赖:每个父RDD的partition 最多被一个子RDD的 partition使用. 窄依赖分为两类:第一类是一对一的依赖关系,在Spark中用On ...

  2. Django 第一天 开端

    今日内容: 一.HTTP协议 1.HTTP协议简介 参考博客:https://www.cnblogs.com/clschao/articles/9230431.html 是超文本传输协议 现在使用最广 ...

  3. samba 配置参数详解

    samba 配置参数详解: 一.全局配置参数  workgroup = WORKGROUP说明:设定 Samba Server 所要加入的工作组或者域. server string = Samba S ...

  4. Linux下搭建iSCSI共享存储的方法 Linux-IO Target 方式 Debian9.5下实现

    iSCSI(internet SCSI)技术由IBM公司研究开发,是一个供硬件设备使用的.可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行SCSI协议,使其能够在诸如高速 ...

  5. AtCoder Grand Contest 038 题解

    传送门 这场表现的宛如一个\(zz\) \(A\) 先直接把前\(b\)行全写成\(1\),再把前\(a\)列取反就行 const int N=1005; char mp[N][N];int n,m, ...

  6. maven ssm 编译异常记录:

    maven ssm 编译异常记录: javax.servlet.jsp 解决: 清除 tomacat libraries 修改 pom 文件 <dependency> <groupI ...

  7. Android Studio—增删改查—登录功能

    SQLite数据库的常用操作: create table if not exists 表名(字段1 类型(长度),字段2 类型(长度),...)//       建表 drop table if ex ...

  8. 急急急,tp5的验证码不显示

    本地环境phpstudy,使用composer安装tp5,按照看云<ThinkPHP5.0完全开发手册>验证码配置,就是不显示验证码. 使用:<div>{:captcha_im ...

  9. DM中将有缝隙的面体缝合为实体

    原版视频下载地址链接: https://pan.baidu.com/s/1mi0NOeO 密码: nw7g

  10. .NET Core教程--给API加一个服务端缓存啦

    以前给API接口写缓存基本都是这样写代码: // redis key var bookRedisKey = ConstRedisKey.RecommendationBooks.CopyOne(book ...