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. docker 挂载实现容器配置更改为外部文件

    docker安装镜像后,每个服务都是独立的容器,容器与容器之间可以说是没关系,隔离独立的. 而且虚拟出来的这些容器里面的基本安装工具都是没有的,比如vi,vim等等.需要使用,还得安装处理. 那么我们 ...

  2. 洛谷 P3375 【模板】KMP字符串匹配 题解

    KMP模板,就不解释了 #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  3. .Net Core WebApi实现跨域

    .Net Core 需要引用一个包  Microsoft.AspNetCore.Cors 让接口实现跨域,需要配置两个地方. 一.Startup.cs 这里需要配置两个地方 public void C ...

  4. 06.volatile关键字

    volatile volatile关键字的主要作用是使变量在多个线程间可见 使用方法: private volatile int number=0; 图示: 两个线程t1和t2共享一份数据,int a ...

  5. 飞扬的小鸟 DP

    飞扬的小鸟 DP 细节有点恶心的DP,设\(f[i][j]\)表示横坐标为\(i\)(从\(0\)开始)高度为\(j\)时,屏幕点击的最小次数为\(f[i][j]\),转移便很好写了,这里要注意枚举当 ...

  6. fork()函数 图解

    code #include<stdio.h> #include <getopt.h> #include<iostream> #include<string&g ...

  7. 编译器错误 CS0540

    编译项目报错:包含类型不实现接口,CS0540 原因:试图在非派生自接口的类中实现接口成员. 解决方案: 删除接口成员的实现,或将接口添加到类的基类列表. 下面的两个示例生成 CS0540: 一. / ...

  8. 五笔字典86版wubi拆字图编码查询

    五笔字典86版 软件能查询以下数据,五笔编码,汉字拆字图,拼音,部首,笔划,笔顺,解释,五笔口诀等等.这些数据只针对单个汉字查询(大概7000字左右).词组查询只支持五笔编码查询(有60000个词组+ ...

  9. 以前进行的程序安装创建了挂起的文件操作(SqlServer2000或SqlServer 2000 SP4补丁安装)

    在安装SqlServer 2000或者SqlServer 2000 SP4补丁时常常会出现这样的提示,从而不能进行安装,即使重新启动了计算机,也还是会有同样的提示.在网上查了一下资料,原来是注册表里记 ...

  10. Sequence contains no elements

    这个错误,在使用List<T>的First函数遇到. Sequence contains no elements? From "Fixing LINQ Error: Sequen ...