Skip to content

Commit e690a31

Browse files
committed
Add solution #235
1 parent dac1f92 commit e690a31

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

0653-two-sum-iv---input-is-a-bst.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|
6464
232|[Implement Queue using Stacks](./0232-implement-queue-using-stacks.js)|Easy|
6565
234|[Palindrome Linked List](./0234-palindrome-linked-list.js)|Easy|
66+
235|[Lowest Common Ancestor of a Binary Search Tree](./0235-lowest-common-ancestor-of-a-binary-search-tree.js)|Easy|
6667
237|[Delete Node in a Linked List](./0237-delete-node-in-a-linked-list.js)|Easy|
6768
242|[Valid Anagram](./0242-valid-anagram.js)|Easy|
6869
263|[Ugly Number](./0263-ugly-number.js)|Easy|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 235. Lowest Common Ancestor of a Binary Search Tree
3+
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
4+
* Difficulty: Easy
5+
*
6+
* Given a binary search tree (BST), find the lowest common ancestor (LCA) of two
7+
* given nodes in the BST.
8+
*
9+
* According to the definition of LCA on Wikipedia: “The lowest common ancestor is
10+
* defined between two nodes p and q as the lowest node in T that has both p and q
11+
* as descendants (where we allow a node to be a descendant of itself).”
12+
*/
13+
14+
/**
15+
* Definition for a binary tree node.
16+
* function TreeNode(val) {
17+
* this.val = val;
18+
* this.left = this.right = null;
19+
* }
20+
*/
21+
22+
/**
23+
* @param {TreeNode} root
24+
* @param {TreeNode} p
25+
* @param {TreeNode} q
26+
* @return {TreeNode}
27+
*/
28+
var lowestCommonAncestor = function(root, p, q) {
29+
if (p.val > root.val && q.val > root.val) {
30+
return lowestCommonAncestor(root.right, p, q);
31+
}
32+
if (p.val < root.val && q.val < root.val) {
33+
return lowestCommonAncestor(root.left, p , q);
34+
}
35+
return root;
36+
};

0 commit comments

Comments
 (0)