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 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 nodes2and8is6.
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 nodes2and4is2, 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的更多相关文章
- [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.
http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...
- 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 ...
- 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 ...
- 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 利用二 ...
- 【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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 使用Jackson的@JsonFormat注解时出现少了 8 个小时
比如数据库存的日期是2018-01-05,转成json则变成了2018-01-04 解决办法: @JsonFormat(pattern="yyyy-MM-dd") public D ...
- oracle: jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
https://www.cnblogs.com/mmlw/p/5808072.html org.mybatis.spring.MyBatisSystemException: nested except ...
- 集合(Collection)类
集合(Collection)类是专门用于数据存储和检索的类.这些类提供了对栈(stack).队列(queue).列表(list)和哈希表(hash table)的支持.大多数集合类实现了相同的接口. ...
- LightOJ - 1311 - Unlucky Bird(相遇问题)
链接: https://vjudge.net/problem/LightOJ-1311 题意: A bird was flying on a train line, singing and passi ...
- Java中装箱和拆箱的代码
建议使用1.5或以上的jdk运行, //装箱 值类型到引用类型 int i = 10; Object object =i; System.out.println(object); / ...
- am335x system upgrade rootfs for bridge-utils cross compile (十四)
bridge-utils移植 [目的] 移植bridge-utils的目是在AM335X开发板上使用bridge功能. [环境] 1. Ubuntu 16.04发行版 2. MC183平台 3. ...
- regedit系统注册表,msconfig系统配置
msconfig msconfig即系统配置实用程序,是Microsoft System Configuration的缩写.是在开始菜单里运行中输入然后确认就可以找到程序开启或者禁用, 可以帮助电脑禁 ...
- 下载 Java
官网:https://www.java.com 官网可以下载到最新版本,如果需要下载旧版本的,可以访问: http://www.oracle.com/technetwork/java/archive- ...
- AWS服务器上安全组端口设置和访问的问题
在搭建测试环境时使用AWS服务器环境,AWS EC2需要设置安全组开放端口,如果端口未进行授权则不允许访问,后台授权界面如下: 1.查看某个端口是否在AWS后台被开放,并允许访问: netstat - ...
- snprintf用错了快10年…
int snprintf(char *str, size_t size, const char *format, ...); 从用snprintf开始,size参数一直传的都是buff_size-1, ...