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. VS调试web api服务

    vs2013开发web api service时,使用vs开发服务器调试没有问题,但将项目放到另一台电脑调试(vs2010),总会提示 无法再以下端口启动asp.net开发服务器 错误:通常每个套接字 ...

  2. 针对jar里面的图片不显示问题

    做了一个html生产pdf案例. 然后把图片放到resource/static/model/img下面,生成jar包运行,发现图片不显示, 发现html里面的src必须是http域名开头的图片. 下面 ...

  3. 开放API接口安全处理

    一.开放API接口定义 顾名思义,开放出来给其他人调用的API接口就是开放API接口.例如,短信接口.邮件接口. 二.开放API的弱点 数据窃取 用户的密码等信息被不轨之人窃取,登录账号发布敏感信息, ...

  4. php自定义函数之匿名函数

    所谓匿名,就是没有名字. 匿名函数,也就是没有函数名的函数.直线电机参数 匿名函数的第一种用法,直接把赋数赋值给变量,调用变量即为调用函数. 匿名函数的写法比较灵活. 1.变量函数式的匿名函数 < ...

  5. mysql 运算操作符

    Name Description AND, && Logical AND = Assign a value (as part of a SET statement, or as par ...

  6. Ubuntu shell系统的环境变量

    1.系统环境变量env命令查看 1)利用export命令导出环境变量 export PS1 导出PS1 添加路径 export PATH=$PATH:/home/daokr/myfile $ sudo ...

  7. coturn编译运行在Windows平台

    turn 编译安装到Windows平台 https://www.webrtc-experiment.com/docs/TURN-server-installation-guide.html#windo ...

  8. idea使用/***/单行注释格式化后会显示多行,如何能保持单行显示

  9. php 压缩文件

    <?php $zip = new ZipArchive; $myfile = fopen("test.zip", "w"); chmod(); if ($ ...

  10. DevOps-如何构建持续交付流水线

    引言 DevOps 是一套实践方法,在保证高质量的前提下缩短系统变更从提交到部署至生产环境的时间,其中持续集成和持续交付是 DevOps 里面非常重要的一环.本文讲述了达到自动化持续交付需要做的准备工 ...