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. CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs

    题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  i < k\\ ...

  2. Vector(同步)和Arraylist(异步)的异同

    //  同步  异步  //1  同步  //2  异步  //未响应 = 假死  占用内存过多  内存无法进行处理  //请求方式:同步    异步  //网页的展现过程中:1 css文件的下载  ...

  3. DVWA-文件包含漏洞

    本周学习内容: 1.学习web安全深度剖析: 2.学习安全视频: 3.学习乌云漏洞: 4.学习W3School中PHP: 实验内容: 进行DVWA文件包含实验 实验步骤: Low 1.打开DVWA,进 ...

  4. C结构体指针的初步使用

    #include <stdio.h> #include <string.h> struct Books { char title[50]; //char author[100] ...

  5. (9)打鸡儿教你Vue.js

    样式绑定 设置元素的样式 用 v-bind 来设置样式属性 class 与 style 是 HTML 元素的属性 <div v-bind:class="{ active: isActi ...

  6. go error=216编译错误

    CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版本不兼容.请查看计算机的系统信息,了解是否需要 x86 (32 位)或 x64 (64 位)版本的程序, ...

  7. Hbase监控指标项

    名词解释 JMX:Java Management Extensions,用于用于Java程序扩展监控和管理项 GC:Garbage Collection,垃圾收集,垃圾回收机制 指标项来源 主机名 u ...

  8. 怎么用switchhost

    第一步:打开exe, 第二部:在 My hosts 里面直接添加路径 106.75.131.183 npm.kuaizitech.cn 第三部 :打开my hosts 保护好眼睛,早睡早起,多运动,k ...

  9. return关键字

    注意:如果一个函数的返回值类型是具体的数据类型,那么该函数就必须要保证在任意情况下都保证有返回值(除了返回值类型是void)     return  关键字的作用: 1   返回数据给函数的调用者. ...

  10. JVM命令行参数

    root@ubuntu-blade2:/sdf/jdk# javaUsage: java [-options] class [args...] (to execute a class) or java ...